Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ashwinr87

macrumors member
Original poster
Mar 9, 2011
81
0
Hi,

I would like to know if it is possible for me to scroll a UIScrollView on the click of a UIButton and if so, would someone be able to tell me how to.

currently I am using the below code to see if in the scrollview more content is there to its left and if it is there, display an image which would tell the users that there is more content if they scroll to the left.
I would like to implement a functionality where I add a UIButton instead of the image and when more content is available on the left and when the user clicks the button, the scrollview would scroll to its left.

Code:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 {
        if (scrollView1.contentOffset.x == scrollView1.contentSize.width - scrollView1.frame.size.width) 
        {        
            // reached the right
            self.imageView.hidden = YES;
        }
        
        else
        {
            self.imageView.hidden = NO;
        }
    }

It would be great if someone could help me out on this.
 
thanks for the reply.. I got it to work..
I have another doubt.. how do I stop the scrolling on button click as soon as the end of the contentview is reached?

this is how I got the scrolling on button click possible
Code:
CGFloat xOffset = scrollView.contentOffset.x;
    CGFloat yOffset = scrollView.contentOffset.y;
    
    if (scrollView.contentOffset.x != scrollView.frame.origin.x)
    {
        [scrollView setContentOffset:CGPointMake(xOffset + 100, yOffset) animated:YES];
    }

The contentOffset property is read/write. You can just set it to instantly scroll to where you want to be. If you want to animate that change there is the documented setContentOffset:animated: method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.