【问题标题】:Scrolling the array of images using the CABasicAnimation使用 CABasicAnimation 滚动图像数组
【发布时间】:2012-04-27 14:06:53
【问题描述】:

我必须一张一张地滚动图像。以下是我正在使用的代码

_imageView 是 UIImageView,imagesArray 是带有对象数组的 NSArray。

   _imageView.animationImages=imagesArray;
   _imageView.animationDuration=10;
    [_imageView startAnimating];
     
    CABasicAnimation *scrollText;
    scrollText=[CABasicAnimation animationWithKeyPath:@"position.x"];
    scrollText.duration = 10.0;
    scrollText.speed= 3; 
    scrollText.repeatCount = 0;
    scrollText.autoreverses = NO;
    
    scrollText.fromValue = [NSNumber numberWithFloat:500];
    scrollText.toValue = [NSNumber numberWithFloat:-200.0];
    
    [[_imageView layer] addAnimation:scrollText forKey:@"scrollTextKey"];

我可以看到图像正在滚动,并且正在一张一张地变化。 但这些图像都在不断变化。当图像离开框架/屏幕时,我想一张一张地更改图像。任何人都可以提出一些想法吗?

【问题讨论】:

    标签: ios xcode cabasicanimation


    【解决方案1】:

    我不太确定你在问什么。我想你是说你想创建一个动画,其中一系列图像从屏幕底部开始,然后动画到顶部,然后离开屏幕。

    为此,您可能希望创建一个动画,将图像从底部的屏幕外移动到顶部的屏幕外,然后用不同的图像循环该动画几次。像这样。

    定义一个实例变量imageNumber。将其设置为零并调用 animateImage(如下)来启动动画。

    代码可能如下所示:

    -(void) animateImage;
    {
      _imageView.image = [imagesArray objectAtIndex: imageNumber];
      CGPoint imageCenter = _imageView.center;
      imageCenter.y = 500;
      _imageView.center  = imageCenter;
      [UIView animateWithDuration: 10
        delay: 0.0
        options: UIViewAnimationOptionCurveLinear
        animations: 
        ^{
           CGPoint newImageCenter = _imageView.center;
           newImageCenter.y = -200;
           _imageView.center  = imageCenter;
        completion:
        ^{
          imageNumber++;
          if (imageNumber < [imagesArray count])
            [self animateImage];
        }
      ];
    }
    

    【讨论】:

    • 谢谢。那会帮助我。我已经使用 CALayer 和 CABasicAnimation 实现了这一点。我也会尝试这种方式,非常少 LOC。再次感谢。
    • LOC = 代码行数?
    猜你喜欢
    • 2023-04-09
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    相关资源
    最近更新 更多