我想水平添加滚动视图。它应该包含几个按钮。每当Itouch按钮时,它应该滚动到中心。就像这样
当我触摸按钮时,它应滚动到此箭头位置。我怎么能这样做?
请帮帮我
答案 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);
}