0
votes

I have a scrollView with paging enabled.

I want to scroll to the next page programmatically. My code

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

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

But what happens is that the scrolling starts and then, at 80~90% of its completion, it scrolls back to the initial page. It is more a "go to (n+1) and immediately return to n" animation than a "go to (n+1)" animation...

The problem might be some interaction between scrollRectToVisible and the paging mode.

1
(currentPage+1)*scroll.frame.width; set this to content offset.Blind Ninja

1 Answers

-1
votes

Its so simple i am giving you a method use this directly only make object in header file

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

after create method 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;
}