2012-08-28 203 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:動畫:當計時器觸發時。

相關問題