views:

61

answers:

2

I have a UIScrollView that has 10 images from an array. I need to scroll left or right to the next or previous image using the button

button scrollview button
   <       [ ]      >
+2  A: 

set the contentOffset property of the scroll view to change the scroll position.

Ben Gottlieb
example please?
AWright4911
I've tried: pos -=1; [scrollView scrollRectToVisible:CGRectMake(pos*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];but this doesnt center the image
AWright4911
scrollView.contentOffset = CGPointMake(100, 0);
Ben Gottlieb
A: 

Well, this is how I was able to properly scroll a UIScrollView using a UIButton The IF statements here assure that the scrollview wont go beyond the bounds of My NSArray of images.

#pragma mark -
#pragma mark Use a UIButton to scroll a UIScrollView Left or Right

-(IBAction)scrollRight{ 
    if(pos<9){pos +=1;
        [scrollView scrollRectToVisible:CGRectMake(pos*scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
        NSLog(@"Position: %i",pos);
    }
}
-(IBAction)scrollLeft{  
    if(pos>0){pos -=1;
        [scrollView scrollRectToVisible:CGRectMake(pos*scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
        NSLog(@"Position: %i",pos);
    }
}
AWright4911