iOS UIImageView中逐帧播放的问题

时间:2012-09-26 18:54:47

标签: ios frame nstimer uiviewanimation

我正在尝试在我的iOS应用中显示动画,但由于某些原因我不想使用默认的iOS UIAnimation,如:

ImageView.animationImages = arrayOfImages;

[ImageView startAnimating];

我安排了一个定时器,每个0.015秒调用一次AnimationTick方法,这是该方法的实现:

-(void)animationTick{
if ( self.frameNumber > 0 && self.frameNumber < 19 )
{
    self.c2cAnimation = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 700, 1000)];
    self.c2cAnimation.image = [UIImage imageNamed:[NSString stringWithFormat:@"L6_anim_%d.png",self.frameNumber]];
    if ( self.frameNumber == 1 )
    {
        [self.OverPlayPaper addSubview:self.c2cAnimation];
    }
    self.frameNumber = self.frameNumber + 1 ;
}}

但是当我构建并运行应用程序时,屏幕上绝对没有任何内容......

我做错了吗?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

首先,不要多次分配任何对象。

你必须在开始计时之前写self.c2cAnimation = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 700, 1000)];

其次,请确保您已初始化self.frameNumber = 1;。如果你没有初始化它,那么在你启动计时器之前写下self.frameNumber = 1;

第三,确保你有正确的启动计时器或使用它....

[NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(animationTick) userInfo:nil repeats:YES];

您将获得所需的输出

内容::

//START TIMER:

    self.c2cAnimation = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 700, 1000)];
    self.frameNumber = 1;
    [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(animationTick) userInfo:nil repeats:YES];

然后写

//FUNCTION FOR ANIMATION:

-(void)animationTick
{
     if ( self.frameNumber > 0 && self.frameNumber < 19 )
     {
          self.c2cAnimation.image = [UIImage imageNamed:[NSString stringWithFormat:@"L6_anim_%d.png",self.frameNumber]];
          if ( self.frameNumber == 1 )
          {
               [self.OverPlayPaper addSubview:self.c2cAnimation];
          }
          self.frameNumber = self.frameNumber + 1 ;
     }
}