scrolling a UIScrollView on the click of a button

Discussion in 'iOS Programming' started by ashwinr87, Jun 17, 2011.

  1. ashwinr87 macrumors member

    Joined:
    Mar 9, 2011
    #1
    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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. ashwinr87 thread starter macrumors member

    Joined:
    Mar 9, 2011
    #3
    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];
        }
     

Share This Page