【问题标题】:Animation of scrolling in collectionview in iosios中collectionview中的滚动动画
【发布时间】:2016-04-12 06:35:45
【问题描述】:

我有一个集合视图,其中我有 10 个从 web 服务加载的图像。我想将该图像视图从第一个位置自动滚动到最后一个位置,然后在该页面出现时连续滚动到第一个位置。 我使用下面的方法

-(void)scrollSlowly 
{
    CATransition *transition = [CATransition animation];
    transition.duration = 4.0f;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [UIView animateWithDuration:5.0f
                          delay:3.0f
                        options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{

                         [self.CollectionView setContentOffset:CGPointMake(CGFLOAT_MAX ,0)];

                     }
                     completion:nil];
    [self.CollectionView.layer addAnimation:transition forKey:@"transition"];
}

-(void)scrollSlowlyToPoint 
{
   self.CollectionView.contentOffset = self.scrollingPoint;
    //    Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint.
   if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint))
   {
       [self.scrollingTimer invalidate];
   }
    //    Going one pixel to the right.
   self.scrollingPoint = CGPointMake(self.scrollingPoint.x+1, self.scrollingPoint.y);
}

我想在 Objective-C 中水平自动滚动。提前致谢

【问题讨论】:

  • 你为什么不用- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
  • 是的,我也使用它,但它不会让我重复缓慢滚动...@sourcer

标签: ios objective-c uiscrollview uicollectionview


【解决方案1】:

创建一个自定义的 UICollectionViewCell 类,创建并连接一个用于图像视图的插座。

在.h文件中:

    NSMutableArray *imgArr;
    NSInteger testIndexPath;

我猜你可以实现 UICollectionView 的数据源方法。

在 cellForItemAtIndexPath 中添加图像到 imageview 后添加这一行

    testIndexPath=indexPath.row;//this will give you the indexPath for cell

现在在你的 .m 文件中添加这两个方法:

   -(void)autoScroll{
    CGFloat point = self.collectionVw.contentOffset.x;
    CGFloat lo = point + 1;
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.collectionVw.contentOffset = CGPointMake(lo, 0);
}completion:^(BOOL finished){
    if (testIndexPath==8)
    {
        [self autoScrollReverse];
    }
    else{
      [self autoScroll];
    }
  }];
 }

  -(void)autoScrollReverse{
    CGFloat point = self.collectionVw.contentOffset.x;
    CGFloat lo = point - 1;
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.collectionVw.contentOffset = CGPointMake(lo, 0);
}completion:^(BOOL finished){
    if(testIndexPath == 0){
        [self autoScroll];
    }else{
       [self autoScrollReverse];

    }

  }];
 }

 //Call [self autoScroll] in your viewDidLoad

【讨论】:

  • 我在collectionview的单元格中有我的imageview....我怎样才能通过上面的代码实现水平连续滚动动画...无法得到它..@Akash KR
  • 这与我上面提到的@Akash KR 使用的方法相同
  • 好吧,我有一个逻辑..检查 indexPath,如果 indexPath 等于 count 然后使用这个 [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];否则反转它。
  • 你能帮我写一个代码吗??告诉你你告诉bt dnt知道怎么做...因为我试图重复它bt它不会在去之后从左到右从右到左......而且当它从右到左时虽然图像仍然显示我空白滚动并且图像消失了@Akash KR
【解决方案2】:

从上到下和从下到上滚动索引的更好方法。

这将移动到索引 10 意味着从上到下滚动

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:9 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];

再次从底部滚动到顶部,移动到索引 0

 [UIView animateWithDuration:3
                      delay:0.1
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
                 }
                 completion:nil];

您也可以像上面一样在 UIVIew 动画中设置从上到下的滚动。 如果每次页面出现都需要动画,请在 ViewWillAppear 中设置第一个方法,在 ViewDidAppear 中设置第二个方法

【讨论】:

    【解决方案3】:

    请应用这个。可能对你有帮助

       - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
    
        [UIView animateWithDuration:0.2 animations:^{
         cell.transform = CGAffineTransformMakeTranslation(0.0, -50);
        }];
        [self delayBy:0.5 code:^{ // call block of code after time interval
        [UIView animateWithDuration:0.3 animations:^{
            cell.transform = CGAffineTransformIdentity;
        }];
      }];
      return cell;
    }
    

    【讨论】:

    • [self delayBy :];这个 self 方法丢失了你可以通过方法添加延迟吗.. 谢谢
    【解决方案4】:

    这可能有助于解决您的问题:

    int maxDelay = 4;
    int delayInSeconds = arc4random() % maxDelay;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //your codes//
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-30
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多