iphone - UIScrollView和scrollRectToVisible :animated:

标签 iphone uiscrollview

我有一个分页 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 示例代码的完整副本位于此处:

https://github.com/jogu/WWDC-2010/tree/master/PhotoScroller

关于iphone - UIScrollView和scrollRectToVisible :animated:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3722787/

相关文章:

iphone - 向 UIScrollview 添加 View

ios - UIScrollView 在 subview 内部触摸时垂直滚动

iphone - 无法从 ios 5 在 sqlite 中插入数据

ios - iOS 的 IAP 收据验证

ios - 如何在 ios 中处理 AVAudioplayer 中的音量 slider 位置

android - Socket.io 适用于桌面版 safari 和 chrome,但不适用于移动设备

swift - 同时接收scrollViewDidScroll和UIGestureRecognizer触摸事件

iphone - 使用 UIGestureRecognizer 中断滚动/动画 UIScrollView

iphone - 如何使用 iCloud 同步我的 sqlite 数据库和驻留在应用程序库文件夹中的文件夹中的图像

iphone - iPhone 应用程序的分发(App Store)版本中出现错误