我有一个UIScrollView,它有10个来自数组的图像。我需要使用按钮
向左或向右滚动到下一个或上一个图像button scrollview button
< [ ... ] >
答案 0 :(得分:9)
嗯,这就是我能够使用UIButton正确滚动UIScrollView的方法 这里的IF语句确保scrollview不会超出My NSArray图像的范围。
#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);
}
}
答案 1 :(得分:3)
设置滚动视图的contentOffset属性以更改滚动位置。