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开发者_JAVA百科 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.
- (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.
That's pretty easy, I think. Use:
[scrollView1 setContentOffset:CGPointMake(x, 0) animated:YES];
Where X is the value of scrollView1.contentOffset.x + scrollView1.contentSize.width.
for Objective C
use:
-(IBAction)adjustScroll:(UIButton *)sender{
CGFloat xOffset = scrollView1.contentOffset.x;
CGFloat yOffset = scrollView1.contentOffset.y;
[scrollView1 setContentOffset:CGPointMake(xOffset + scrollView1.contentSize.width, yOffset) animated:YES];
}
for swift 4.2
use:
@IBAction func adjustScroll(sender:UIButton){
let xOffset = scrollView1.contentOffset.x
let yOffset = scrollView1.contentOffset.y
let customOffset = CGPoint(x: xOffset + scrollView1.contentSize.width, y: yOffset)
scrollView1.setContentOffset(customOffset, animated: true)
}
精彩评论