我有一个分页 UIScrollView,可以分页浏览多个全屏图像。我根据 Apple 示例代码,在 ScrollView 页面通过图像集合时动态平铺页面、对 UIView 进行排队和出队。
我有一个工具栏按钮,调用scrollRectToVisible:animated: 将 UIScrollView 移动到特定图像。效果很好。
问题是,如果您在 UIScrollView 中进行一次触摸,它会滚动回触摸按钮之前显示的页面,并且调用scrollRectToVisible:animated: 方法会 ScrollView 。
如果您的触摸正在移动,UIScrollView 将按预期滚动,并且后续触摸不会导致 UIScrollView 移回到原始页面。
如何防止这种行为?
谢谢
jk
最佳答案
您需要使用内容偏移量而不是scrollRectToVisible,例如:
[pagingScrollView setContentOffset:[self offsetForPageAtIndex:page] animated:YES];
其中 offsetForPageAtIndex 如下所示:
- (CGPoint)offsetForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGPoint offset;
offset.x = (pagingScrollViewFrame.size.width * index);
offset.y = 0;
return offset;
}
这基于 WWDC 2010 中的 Apple“photoscroller”示例代码,其中有一个如下所示的frameForPagingScrollView:
- (CGRect)frameForPagingScrollView {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
该版本 Photoscroller 示例代码的完整副本位于此处:
关于iphone - UIScrollView和scrollRectToVisible :animated:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3722787/