开发者

How can I set a maximum on the number of pages in a TTLauncherView?

开发者 https://www.devze.com 2023-02-24 16:48 出处:网络
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

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 scro开发者_高级运维llViewSubview];
            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];
    }
}


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


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];

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号