使用 CABasicAnimation 滚动图像数组 [英] Scrolling the array of images using the CABasicAnimation

查看:25
本文介绍了使用 CABasicAnimation 滚动图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to scroll the images one by one. THe following are the code which i am using

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

_imageView is UIImageView and imagesArray is the NSArray with array of objects.

   _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"];

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

I can see that images are scrolling and it is changing one by one. But these images are changing irrespectively. I want to change the image one by one when it leaves the frame/Screen. Can any one suggest some idea ?

推荐答案

我不太清楚你在问什么.我认为您是在说您想要创建一个动画,其中一系列图像从屏幕底部开始,向上移动到顶部,然后离开屏幕.

I'm not quite sure what you are asking. I think you're saying that you want to create an animation where a series of images start at the bottom of the screen and animate up to the top, and off the screen.

为此,您可能希望创建一个动画,将图像从底部的离屏动画设置为顶部的离屏动画,然后使用不同的图像多次循环播放该动画.像这样的东西.

To do that, you'd probably want to create an animation that animates an image from off-screen on the bottom to off-screen on the top, and then loop that animation several times with different images. Something like this.

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

Define an instance variable imageNumber. Set it to zero and call animateImage (below) to start the animation.

代码可能如下所示:

-(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];
    }
  ];
}

这篇关于使用 CABasicAnimation 滚动图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆