如何设置 TTLauncherView 中的最大页面数?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在
TTLauncherView
中覆盖beginEditing
是最好的选择。由于您实际上只会接触一种方法(并且该方法中只有几行),因此在时机成熟时升级它应该不会太糟糕。由于该方法显式添加了额外的页面,因此我不确定如何在不编辑特定代码段的情况下解决它I think overriding
beginEditing
inTTLauncherView
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正如 Andrew Flynn 在他的回答中所建议的那样,我能够通过子类化和覆盖
beginEditing
方法来删除它进入编辑模式时添加的额外页面TTLauncherView
来使其工作。我遇到的一个问题是我不知道如何删除在子类上调用(私有)方法
updateContentSize
时收到的警告。我尝试将其转换为 id,但这并没有消除警告。是否可以?编辑:我能够通过使用
performSelector
将消息发送到私有类来删除警告。 (我之前创建了一个类别方法performSelector:withInt
,它包装了NSInitation
,以便我可以通过performSelector
方法传递基元,这使得这非常方便.)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 pageTTLauncherView
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 toid
, 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 methodperformSelector:withInt
that wrapsNSInvocation
so that I can pass primitives viaperformSelector
methods, which makes this very convenient.)