开发者

UIScrollView with UIButtons 'skipping' when finger drags outside of its superviews bounds

开发者 https://www.devze.com 2023-04-04 19:23 出处:网络
I have a scrollView set up as the titleView of a navigationBar. The contentView of the scrollView has 4 buttons in it, all laid out dynamically. The buttons need to cancel with drags and scrolling, so

I have a scrollView set up as the titleView of a navigationBar. The contentView of the scrollView has 4 buttons in it, all laid out dynamically. The buttons need to cancel with drags and scrolling, so I've subclassed them to setEnabled:NO on themselves when touchesMoved.

When I try to scroll the scrollView, as soon as I pass over the bounds of the superview (i.e., passing the bottom or top bounds navigationBar), the scrollview 'skips' 8 or 11 pixels, depending on if its the top or the button. It has something to do with the UIButtons, because when I just use UILabels the problem doesn't persist.

Relevant code:

Scrollview Initialization:

    menuWidth = 196;
    rowHeight = 40;
    rowMargin = self.frame.size.height - rowHeight;

    //drop down menu stuff
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];

    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(self.frame.size.width, (self.frame.size.height)*choices.count);
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.clipsToBounds = YES;
    scrollView.canCancelContentTouches = YES;
    scrollView.delaysContentTouches = YES;
    scrollView.delegate = self;
    [self layoutMenu];
    [self addSubview:scrollView];
    [scrollView release];

Button initialization (-layoutMenu):

SKScrollableButton *tempButton;
NSString *text;
for (int i = 0; i < choices.count; i++) {
    text = (NSString*)[choices objectAtIndex:i];
    if (i == 0) {
        text = [text stringByAppendingString:downArrow];
    }
    CGRect labelRect = CGRectMake(0, i*(rowHeight + rowMargin), menuWidth, rowHeight - 7);

    tempButton = [[SKScrollableButton alloc] initWithFrame:labelRect];
    [tempButton setTitle:text forState:UIControlStateNormal];
    [tempButton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
    [tempButton addTarget:self action:@selector(buttonUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:tempButton];
    [tempButton release];
}

No events get called on the buttons when the skipping happens, 开发者_运维知识库and only normal-seeming scrollViewDidScroll delegate calls happen on the scrollView.

I've tested and the skipping definitely happens when passing the bounds of the superview, not the bounds of the scrollView itself.

0

精彩评论

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