4

我想滚动我的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];         
}

提前感谢您的回答

4

4 回答 4

18

尝试并手动设置新位置。

目标-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];

斯威夫特 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)
于 2012-08-28T15:02:54.590 回答
5

你可以使用[scrollView setContentOffset:CGPointMake(x, y) animated:YES];方法。如果您的 scrollView 水平滚动,那么您需要相应地设置 x 值,否则设置 y 值。

于 2014-01-20T19:48:36.343 回答
3

您可以使用 Obj-C 中的以下内容滚动到滚动视图中的某个点

[scrollView setContentOffset:CGPointMake(x, y) animated:YES];

或斯威夫特

scrollView.contentOffset = CGPoint(x: x, y: y)

要使用 UIScrollView 进行幻灯片放映,您可以在滚动视图中排列所有图像,设置重复计时器,然后 -setContentOffset:animated: 当计时器触发时。

于 2015-10-08T07:37:36.640 回答
0
UIButton *locatorbutton_frame=(UIButton *)[scrollviewoutlet viewWithTag:numberglobal];
float width = scrollviewoutlet.frame.size.width;
float height = scrollviewoutlet.frame.size.height;
float newPosition = scrollviewoutlet.contentOffset.x-locatorbutton_frame.frame.size.width;//+width;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);

[scrollviewoutlet scrollRectToVisible:toVisible animated:YES];

我发现它工作正常 locatorbutton_frame 是我添加到滚动视图的按钮。

于 2014-12-09T11:34:33.017 回答