2012-08-28 108 views
4

我想滚动我的UIScrollView(水平)。但是,当按下按钮时,它会移到滚动视图的最左侧。我想滚动它3,4个像素,当我再次按下滚动另一3,4像素提前如何以编程方式滚动UIScrollVIew

- (IBAction)leftScroll:(id)sender 
{ 
    CGPoint pt; 
    pt.x = 1; 
    pt.y = 0; 
    UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:5]; 
    [sv setContentOffset:pt animated:YES];   
} 

谢谢您的回答

回答

17

试试吧,手动设置一个新的位置。

目标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) 
+0

伟大的工作。 但我想知道哪个变量的值应该改变,以便滚动少于这个? – OOkhan

+0

scrollView.contentOffset.x +? –

+0

非常感谢帮助我的人。 – OOkhan

5

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

+0

这是现在更有效的方法。 –

+0

滚动结束时会被告知,这个委托方法是你的朋友:'scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView' –

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是我添加到滚动视图的按钮。

2

您可以滚动到某个时刻,在对象 -

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

或雨燕

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

要使用的UIScrollView做幻灯片下面的滚动视图,你安排在滚动所有图像查看,设置重复计时器,然后-setContentOffset:动画:当计时器触发时。

相关问题