1

我想创建一个里面有按钮的栏。每个按钮宽度为 43 像素。我想启用此滚动视图页面,但我不知道如何将页面宽度设置为 43 像素,因此它将恰好停在每个按钮的中心。

我试过这样做:

#define scrollViewButtonWidth 43

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"x offset = %f",scrollView.contentOffset.x);

    int currentX = scrollView.contentOffset.x;
    int wantedX;

    if (currentX % scrollViewButtonWidth < scrollViewButtonWidth / 2) {
        wantedX = (currentX / scrollViewButtonWidth) * scrollViewButtonWidth;
    }else{
        NSLog(@"%d",(currentX / scrollViewButtonWidth));
        wantedX = ((currentX / scrollViewButtonWidth) * scrollViewButtonWidth) + scrollViewButtonWidth;
    }

    [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
    [UIScrollView setAnimationDuration:0.3f];
    [scrollView setContentOffset:CGPointMake(wantedX, 0)];
    [UIScrollView commitAnimations];

}

我尝试将此代码放入 scrollview 的每个委托方法中,但没有做得很好。它并没有像它应该的那样顺利移动——而且看起来真的很糟糕。

任何人都可以帮助我吗?

4

0 回答 0