2012-09-19 207 views
2

我想顯示一個圖像,停留在頁面上5秒,但每次我的滾動視圖滾動時出現。所以顯然我需要結婚的動畫UILabelUIScrollView的一些方法。林不知道哪一個要誠實。另外我有兩個UIScrollView在一個UIViewController所以我不知道我應該設置爲代表。UIScrollView做滾動

以下是動畫我目前所面對的

[UIView animateWithDuration:0.3 animations:^{ // animate the following: 
    pageCountImage.frame = CGRectMake(0, 0, 100, 50); // move to new location 
}]; 
+0

如果動畫發生滾動啓動或停止時? – Mike

+0

動畫應該在滾動停止時開始,然後動畫在5秒內停止。 – CodeGeek123

回答

4

您的視圖控制器可以是兩個滾動視圖的委託。同意@Ravi你可以使用委託參數來確定哪個滾動視圖正在滾動。

聽起來像是你需要打包到有意義的UI幾個動畫:

// hide or show the page count image after a given delay, invoke completion when done 
- (void)setPageCountImageHidden:(BOOL)hidden delay:(NSTimeInterval)delay completion:(void (^)(BOOL))completion { 

    BOOL currentlyHidden = self.pageCountImage.alpha == 0.0; 
    if (hidden == currentlyHidden) return; 

    [UIView animateWithDuration:0.3 delay:delay options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
     self.pageCountImage.alpha = (hidden)? 0.0 : 1.0; 
    } completion:completion]; 
} 

// move the page count image to the correct position given a scroll view content offset 
- (void)positionPageControlForContentOffset:(CGFloat)xOffset { 

    // assume page width is a constant (the width of a page in the scroll view) 
    NSInteger page = xOffset/kPAGEWIDTH; 

    // assume max page is a constant (the max number of pages in scroll view) 
    // scroll positions in the "bounce" will generate page numbers out of bounds, fix that here... 
    page = MAX(MIN(page, kMAXPAGE), 0); 

    // kPAGE_INDICATOR_WIDTH the distance the page image moves between pages 
    // kPAGE_INDICATOR_ORIGIN the page image x position at page zero 
    CGFloat xPosition = kPAGE_INDICATOR_ORIGIN + page * kPAGE_INDICATOR_WIDTH; 

    // assume y position and size are constants 
    CGRect pageIndicatorFrame = CGRectMake(xPosition, kYPOS, kWIDTH, kHEIGHT); 

    // finally, do the animation 
    [UIView animateWithDuration:0.3 animations:^{ 
     self.pageCountImage.frame = pageIndicatorFrame; 
    }]; 
} 

然後在視圖並滾動:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 

    if (scrollView == /* the scroller with the page control */) { 

     [self setPageCountImageHidden:NO delay:0.0 completion:^(BOOL finished) { 
      [self positionPageControlForContentOffset:scrollView.contentOffset.x]; 
      [self setPageCountImageHidden:YES delay:5.0 completion:^(BOOL finished){}]; 
     }]; 
    } 
    // and so on... 
3

你應該實現<UIScrollViewDelegate>。使用方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView並在其中寫入您的動畫代碼。如果你有多個滾動視圖,你可以這樣做:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if(scrollView == myScrollView1) 
     // do something 
    else if (scrollView == myScrollView2) 
     // do something else 
    else 
     // do something else 
}