在iOS 8.1中使用按钮进行水平滚动

时间:2015-02-19 10:03:52

标签: ios uiscrollview

我想水平添加滚动视图。它应该包含几个按钮。每当Itouch按钮时,它应该滚动到中心。就像这样

enter image description here

当我触摸按钮时,它应滚动到此箭头位置。我怎么能这样做?

请帮帮我

2 个答案:

答案 0 :(得分:0)

您应该将滚动条内容偏移x值设置为按钮中间位置加上屏幕宽度:

-(IBAction)buttonPressed:(id)sender{
       UIButton *button = (UIButton *)sender;
       float offsetPointX = button.frame.origin.x - button.frame.size.width/2 + [UIScreen mainScreen].bounds.width;
       yourScrollView.contentOffset = CGPointMake(offsetPointX, yourScrollView.contentOffset.y); 
 }

答案 1 :(得分:-1)

我没有对代码进行测试,但尝试过这样的事情:

-(IBAction)buttonPressed:(id)sender{

   UIButton *button = (UIButton *)sender;

   NSInteger offset = yourScrollView.frame.size.width/2 - button.frame.origin.x - button frame.size.width/2;

   yourScrollView.contentOffset = CGPointMake(0, offset); 
 }
相关问题