UIScrollView:如何在启用分页的情况下滚动到下一页?

时间:2015-08-27 12:49:31

标签: ios uiscrollview paging uiviewanimation

我有一个启用了分页的scrollView。

我想以编程方式滚动到下一页。我的代码

    CGRect newRect = self.scrollView.bounds;
    newRect.origin = CGPointMake(self.scrollView.contentOffset.x + newRect.size.width, 0);

    [self.scrollView scrollRectToVisible:newRect
                                animated:YES];

但是会发生滚动开始然后在完成80%到90%时滚动回到初始页面。它更像是“转到(n + 1)并立即返回到”动画而不是“转到(n + 1)”动画...

问题可能是scrollRectToVisible与分页模式之间的某些互动。

1 个答案:

答案 0 :(得分:-1)

它如此简单我给你一个方法直接使用它只在头文件中生成对象

@property (weak, nonatomic) IBOutlet UIScrollView *scrollViewImages;

创建方法loadImages

之后
-(void)loadImages
{

_scrollViewImages.contentSize = CGSizeMake(arrayAdImages.count*_scrollViewImages.frame.size.width,0);
_scrollViewImages.pagingEnabled=YES;

if(arrayAdImages.count>0)
{

    int xOrigin = 0;

    for(int i =0; i<[arrayAdImages count]; i++)

    {
        NSString*img=[arrayAdImages objectAtIndex:i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;


        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
        button.frame = CGRectMake(xOrigin, 0, 768  ,768);
        }
        else{
        button.frame = CGRectMake(xOrigin, 0, 320  ,320);

        }
        NSString *image=[NSString stringWithFormat:@"%@",img];
         image= [image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"image url tesing %@",image);
        [button setBackgroundImageForState:UIControlStateNormal withURL:[NSURL URLWithString:image]];

        if (i==0) {
            [imgUrlForSharing setImageWithURL:[NSURL URLWithString:image]];
        }


        [_scrollViewImages addSubview:button];
        xOrigin = xOrigin + _scrollViewImages.frame.size.width;



         button.contentMode=UIViewContentModeScaleAspectFill;

    }
}
double delayInSeconds = 1.3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [[_scrollViewImages viewWithTag:1356] removeFromSuperview];
});

_scrollViewImages.showsHorizontalScrollIndicator=false;
_scrollViewImages.showsVerticalScrollIndicator=false;
}