【问题标题】:UIScrollView did ScrollUIScrollView 做了滚动
【发布时间】:2012-09-12 00:59:32
【问题描述】:

我想显示一个在页面上停留 5 秒但每次滚动视图滚动时都会出现的图像。所以很明显我需要为UILabelUIScrollView 的一些方法结合动画。老实说,我不确定使用哪一个。另外我在一个UIViewController 上有两个UIScrollViews,所以我不知道我应该设置什么作为代表。

以下是我现在的动画

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

【问题讨论】:

  • 动画应该在滚动开始还是停止时出现?
  • 动画应该在滚动停止时开始,然后动画在 5 秒后停止。

标签: iphone objective-c ios uiscrollview uiscrollviewdelegate


【解决方案1】:

你应该实现<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
}

【讨论】:

    【解决方案2】:

    您的视图控制器可以是两个滚动视图的代表。同意@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】:

      就像 tableview 你应该配置和控制你的滚动动作。您可以在此处编写控制器的扩展并控制所有滚动操作。例如,您可以检查:

      extension ExampleViewController: UIScrollViewDelegate {
      
          func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
              // your actions here
          }
      
          func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
              // your actions here
          }
      
          func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
              // your actions here
          }
      
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
              // your actions here
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-12
        • 2014-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        相关资源
        最近更新 更多