我想创建一个带有按钮的栏。每个按钮宽度为43像素。我想要启用此scrollview页面,但我不知道如何设置页面宽度为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的每个委托方法中,但是没有做得很好。它没有像它应该的那样平稳地移动 - 而且看起来非常糟糕。
任何人都可以帮助我吗?