如何设置 TTLauncherView 中的最大页面数?

发布于 2024-11-01 23:04:00 字数 2661 浏览 1 评论 0原文

我使用 TTLauncherView 作为我的应用程序的主屏幕,但我只有一页的图标。我怎样才能使 TTLauncherView 不允许您将图标拖动到“下一页”?我想设置最大页数(在本例中为一页。)

(编辑: 长话短说,我对 beginEditing 进行了子类化,请参阅下面的答案。< strong>)

我明白为什么在调用 beginEditing 时会添加额外的页面,但我不想编辑框架代码。 (这使得更新到新版本变得困难。)如果我必须依赖它的实现方式,我也不想子类化和重写该方法。 (如果干净的话,我不反对子类化或添加类别。)

我尝试在我的 TTLauncherViewDelegate< 的回调方法 launcherViewDidBeginEditing 中将 scrollView.scrollEnabled 设置为 NO /code>,但是在编辑模式下不起作用,我不知道为什么。

我尝试在滚动视图中添加一个拦截器 UIView,通过设置 userInteractionEnabled=NO 来拦截触摸事件,效果很好。我仍然必须以某种方式禁用将 TTLauncherItems 拖动到下一页。

我还尝试将滚动视图的 contentSize 设置为其在 launcherViewDidBeginEditing 中的 bounds,但这似乎也不起作用。

有更好的办法吗?

尝试阻止手势:

- (void)setLauncherViewScrollEnabled:(BOOL)scrollEnabled {
        if (scrollEnabled) {
            [self.scrollViewTouchInterceptor removeFromSuperview];
            self.scrollViewTouchInterceptor = nil;
        } else {
            // iter through the kids to get the scrollview, put a gesturerecognizer view in front of it
            UIScrollView *scrollView = [launcherView scrollViewSubview];
            self.scrollViewTouchInterceptor = [UIView viewWithFrame:scrollView.bounds]; // property retains it
            UIView *blocker = self.scrollViewTouchInterceptor;
            [scrollView addSubview:scrollViewTouchInterceptor];
            [scrollView sendSubviewToBack:scrollViewTouchInterceptor];
            scrollViewTouchInterceptor.userInteractionEnabled = NO;
        }
    }

参考:TTLauncherView.m

- (void)beginEditing {
    _editing = YES;
    _scrollView.delaysContentTouches = YES;

    UIView* prompt = [self viewWithTag:kPromptTag];
    [prompt removeFromSuperview];

    for (NSArray* buttonPage in _buttons) {
        for (TTLauncherButton* button in buttonPage) {
            button.editing = YES;
            [button.closeButton addTarget:self action:@selector(closeButtonTouchedUpInside:)
                         forControlEvents:UIControlEventTouchUpInside];
        }
    }

    // Add a page at the end
    [_pages addObject:[NSMutableArray array]];
    [_buttons addObject:[NSMutableArray array]];
    [self updateContentSize:_pages.count];

    [self wobble];

    if ([_delegate respondsToSelector:@selector(launcherViewDidBeginEditing:)]) {
        [_delegate launcherViewDidBeginEditing:self];
    }
}

I'm using TTLauncherView as a sort of home screen for my app and I only have one page's worth of icons. How can I make it so the TTLauncherView won't let you drag icons to "the next page"? I want to set a maximum number of pages (in this case one.)

(EDIT: long story short, I subclassed beginEditing, see the answer below.)

I see where why it adds an extra page when beginEditing is called, but I don't want to edit the framework code. (That makes it hard to update to newer versions.) I'd also prefer not to subclass and override that one method, if I have to rely on how it's implemented. (I'm not against subclassing or adding a category if it's clean.)

I tried setting scrollView.scrollEnabled to NO in the callback method launcherViewDidBeginEditing in my TTLauncherViewDelegate, but that doesn't work while it's in editing mode and I don't know why.

I tried adding a blocker UIView to the scrollview to intercept the touch events by setting userInteractionEnabled=NO, which works OK. I still have to disable the dragging of TTLauncherItems to the next page somehow.

I also tried setting the contentSize of the scrollview to it's bounds in launcherViewDidBeginEditing, but that didn't seem to work either.

Is there a better way?

Tried to block gestures:

- (void)setLauncherViewScrollEnabled:(BOOL)scrollEnabled {
        if (scrollEnabled) {
            [self.scrollViewTouchInterceptor removeFromSuperview];
            self.scrollViewTouchInterceptor = nil;
        } else {
            // iter through the kids to get the scrollview, put a gesturerecognizer view in front of it
            UIScrollView *scrollView = [launcherView scrollViewSubview];
            self.scrollViewTouchInterceptor = [UIView viewWithFrame:scrollView.bounds]; // property retains it
            UIView *blocker = self.scrollViewTouchInterceptor;
            [scrollView addSubview:scrollViewTouchInterceptor];
            [scrollView sendSubviewToBack:scrollViewTouchInterceptor];
            scrollViewTouchInterceptor.userInteractionEnabled = NO;
        }
    }

For reference: TTLauncherView.m:

- (void)beginEditing {
    _editing = YES;
    _scrollView.delaysContentTouches = YES;

    UIView* prompt = [self viewWithTag:kPromptTag];
    [prompt removeFromSuperview];

    for (NSArray* buttonPage in _buttons) {
        for (TTLauncherButton* button in buttonPage) {
            button.editing = YES;
            [button.closeButton addTarget:self action:@selector(closeButtonTouchedUpInside:)
                         forControlEvents:UIControlEventTouchUpInside];
        }
    }

    // Add a page at the end
    [_pages addObject:[NSMutableArray array]];
    [_buttons addObject:[NSMutableArray array]];
    [self updateContentSize:_pages.count];

    [self wobble];

    if ([_delegate respondsToSelector:@selector(launcherViewDidBeginEditing:)]) {
        [_delegate launcherViewDidBeginEditing:self];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-11-08 23:04:00

我认为在 TTLauncherView 中覆盖 beginEditing 是最好的选择。由于您实际上只会接触一种方法(并且该方法中只有几行),因此在时机成熟时升级它应该不会太糟糕。由于该方法显式添加了额外的页面,因此我不确定如何在不编辑特定代码段的情况下解决它

I think overriding beginEditing in TTLauncherView is your best bet. Since you'd only really be touching one method (and only a few lines in that method), upgrading it when the time comes shouldn't be too bad. Since that method explicitly adds the extra page, I'm not sure how you'd get around it w/o editing that specific piece of code

情泪▽动烟 2024-11-08 23:04:00

正如 Andrew Flynn 在他的回答中所建议的那样,我能够通过子类化和覆盖 beginEditing 方法来删​​除它进入编辑模式时添加的额外页面 TTLauncherView 来使其工作。

我遇到的一个问题是我不知道如何删除在子类上调用(私有)方法 updateContentSize 时收到的警告。我尝试将其转换为 id,但这并没有消除警告。是否可以?

编辑:我能够通过使用performSelector将消息发送到私有类来删除警告。 (我之前创建了一个类别方法 performSelector:withInt ,它包装了 NSInitation ,以便我可以通过 performSelector 方法传递基元,这使得这非常方便.)

// MyLauncherView.h
@interface MyLauncherView : TTLauncherView {
    NSInteger _maxPages;
}

@property (nonatomic) NSInteger maxPages;

@end




// MyLauncherView.m
@implementation MyLauncherView
@synthesize maxPages = _maxPages;

- (void)beginEditing {
    [super beginEditing];

    // ignore unset or negative number of pages
    if (self.maxPages <= 0) {
        return;
    }

    // if we've got the max number of pages, remove the extra "new page" that is added in [super beginEditing]
    if ([_pages count] >= self.maxPages ) {
        [_pages removeLastObject];

        [self updateContentSize:_pages.count];   // this has a compiler warning

        // I added this method to NSObject via a category so I can pass primitives with performSelector
        // [self performSelector:@selector(updateContentSize:) withInt:_pages.count waitUntilDone:NO];

    }
}

As Andrew Flynn suggested in his answer, I was able to make it work by subclassing and overriding the beginEditing method to remove the extra page TTLauncherView adds when it goes into editing mode.

One problem I'm having is I can't figure out how to remove the warning I get for calling the (private) method updateContentSize on my subclass. I tried casting it to id, but that didn't remove the warning. Is it possible?

edit: I was able to remove the warning by using performSelector to send the message to the private class. (I had previously create a category method performSelector:withInt that wraps NSInvocation so that I can pass primitives via performSelector methods, which makes this very convenient.)

// MyLauncherView.h
@interface MyLauncherView : TTLauncherView {
    NSInteger _maxPages;
}

@property (nonatomic) NSInteger maxPages;

@end




// MyLauncherView.m
@implementation MyLauncherView
@synthesize maxPages = _maxPages;

- (void)beginEditing {
    [super beginEditing];

    // ignore unset or negative number of pages
    if (self.maxPages <= 0) {
        return;
    }

    // if we've got the max number of pages, remove the extra "new page" that is added in [super beginEditing]
    if ([_pages count] >= self.maxPages ) {
        [_pages removeLastObject];

        [self updateContentSize:_pages.count];   // this has a compiler warning

        // I added this method to NSObject via a category so I can pass primitives with performSelector
        // [self performSelector:@selector(updateContentSize:) withInt:_pages.count waitUntilDone:NO];

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文