0

我有一个uiscrollview。Scrollview 包含多个 uiview 作为子视图。我希望​​分页像动画效果一样滚动视图。我设置了 scrollview.pagingEnabled=NO。

以下是我的代码

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView  {
lastContentOffset = scrollView.contentOffset.x;

}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    ScrollDirection scrollDirection;
    if (self.lastContentOffset >scrollView.contentOffset.x) {
        scrollDirection = ScrollDirectionRight; } else if (self.lastContentOffset < scrollView.contentOffset.x)  {
        scrollDirection = ScrollDirectionLeft;
         }

    if (scrollDirection==ScrollDirectionRight) {
        CGFloat xOffset = scrollview.contentOffset.x;
        CGFloat yOffset = scrollview.contentOffset.y;

        if (scrollview.contentOffset.x != scrollview.frame.origin.x)
        {


            [scrollview setContentOffset:CGPointMake(xOffset- 320, yOffset) animated:YES];                     


        }

        NSLog(@" ScrollDirectionRight custom  x==%f %f", scrollview.contentOffset.x, scrollview.contentSize.width);


    } else if(scrollDirection==ScrollDirectionLeft) {
        CGFloat xOffset = scrollview.contentOffset.x;
        CGFloat yOffset = scrollview.contentOffset.y;

        if ((scrollview.contentOffset.x != scrollview.frame.origin.x) )
        {
            [scrollview setContentOffset:CGPointMake(xOffset + 320, yOffset) animated:YES];

        }
        NSLog(@"ScrollDirectionLeft custom  x==%f %f", scrollview.contentOffset.x, scrollview.contentSize.width);

           }
    }

通过这段代码,我得到了类似分页的效果。但效果并不流畅。

谢谢

4

1 回答 1

0

查看 UIScrollViewDelegate 方法

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

在此回调中,您将修改“targetContentOffset”到您希望看到滚动偏移动画结束的位置。

使用它,您可以使自定义分页实现更加顺畅。

于 2013-05-30T10:46:33.327 回答