我想滚动我的UIScrollView(水平).但是当按下按钮时,它会进入滚动视图的最左侧.我想滚动它3,4像素,当我再次按它时滚动另一个3,4像素
- (IBAction)leftScroll:(id)sender
{
CGPoint pt;
pt.x = 1;
pt.y = 0;
UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:5];
[sv setContentOffset:pt animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
提前感谢您的回答
Har*_*ngh 18
尝试并手动设置新位置.
Objective-C的
float width = CGRectGetWidth(scrollView.frame);
float height = CGRectGetHeight(scrollView.frame);
float newPosition = scrollView.contentOffset.x+width;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[scrollView scrollRectToVisible:toVisible animated:YES];
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
let scrollView = UIScrollView()
let width: CGFloat = scrollView.frame.size.width
let height: CGFloat = scrollView.frame.size.height
let newPosition: CGFloat = scrollView.contentOffset.x + width
let toVisible: CGRect = CGRect(x: newPosition, y: 0, width: width, height: height)
scrollView.scrollRectToVisible(toVisible, animated: true)
Run Code Online (Sandbox Code Playgroud)
你可以使用[scrollView setContentOffset:CGPointMake(x, y) animated:YES];方法.如果您的scrollView水平滚动,则需要相应地设置x值,否则为y值.