UIScrollView在iPhone/iPad上旋转时禁用滚动

Ami*_*tan 19 iphone cocoa-touch uiscrollview uikit

我正在使用UIScrollView和其中的图像作为每页分页一个图像.我在旋转iPhone时遇到问题

当我旋转iPhone然后scrollViewDidScroll(滚动视图委托方法)正在调用.因此,我的分页受到干扰,页码也会发生变化.

解决办法是什么?

Ben*_*oyt 24

Raphaël的答案是对问题的出色描述,以及一个简洁的解决方案.我有完全相同的问题,并最终修复了一个scrollingLocked标志,我在旋转开始前设置为YES(已锁定),并在结束时设置为NO(已解锁).也许比临时更改contentSize稍微不那么苛刻:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
                                duration:(NSTimeInterval)duration
{
    self.photoViewer.scrollingLocked = YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromOrientation
{
    self.photoViewer.scrollingLocked = NO;
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    if (self.scrollingLocked)
    {
        return;
    }
    /* do normal scrollViewDidScroll: stuff */
}
Run Code Online (Sandbox Code Playgroud)

  • @matheszabi,他正在使用BOOL ivar进行追踪.或者,您可以使用`UIScrollView`的`scrollEnabled`内置属性.顺便说一句,这仍然是出于某种原因在最后一张图片上触发`scrollViewDidScroll`. (3认同)

Rap*_*Mor 14

旋转分页时,我发现了一个奇怪的无记录行为UIScrollView.

当滚动视图位于最后一页并且用户改变方向时,OS UIScrollView向后滚动几个像素以补偿高度和宽度之间的差异.

基本上我收到了以下任何页面的电话.

willRotateToInterfaceOrientation:duration
willAnimateRotationToInterfaceOrientation:duration:
didRotateFromInterfaceOrientation:
Run Code Online (Sandbox Code Playgroud)

并在最后一页:

willRotateToInterfaceOrientation:duration
scrollViewDidScroll:
willAnimateRotationToInterfaceOrientation:duration:
didRotateFromInterfaceOrientation:
Run Code Online (Sandbox Code Playgroud)

这也搞砸了我的网页.问题在于,willRotate操作系统还没有更新边界,并且willAnimate你有新界限并可以计算新的大小,但为时已晚......

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    CGSize tempSize = [self.pagingScrollView contentSize];
    NSUInteger padding = abs(pagingScrollView.frame.size.width - pagingScrollView.frame.size.height);
    tempSize.width += padding;
    [self.pagingScrollView setContentSize:tempSize];
    [...]
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{
    CGSize newSize = ... // Compute new content size based on new orientation
    [self.pagingScrollView setContentSize:newSize];
}
Run Code Online (Sandbox Code Playgroud)

这只是一种解决方法,但我在这个问题上花了无数个小时,却找不到一个优雅的解决方案.