更改图像的动画速度系列

狗咖啡

我有一系列图像,我希望它们重复。但是,我希望上半场播放2秒,然后下半场播放5秒。

使用以下命令播放一系列图像非常容易

   - (void) completeCycles:(int) cycles inhalePercentage:(int) inhalePercent exhalePercentage:(int) exhalePercent totalCycleTime:(int) time
   {
    NSMutableArray *textures = [NSMutableArray array];

    for (int i = 0; i < kNumberBreathingImages; i++) {
        NSString *textureName = [NSString stringWithFormat:@"breath%d.png", i];

        for (int i = 0; i < inhalePercent; i++) {
            [textures addObject:[UIImage imageNamed:textureName]];
        }
    }

    for (int i = kNumberBreathingImages - 1; i > 0; i--) {
        NSString *textureName = [NSString stringWithFormat:@"breath%d.png", i];
        for (int i = 0; i < exhalePercent; i++) {
            [textures addObject:[UIImage imageNamed:textureName]];
        }
    }

    self.animationImageView.animationImages = textures;
    self.animationImageView.animationDuration = time;
    self.animationImageView.animationRepeatCount = cycles;
    [self.animationImageView startAnimating];
    }

但是,我希望动画的上半部分花费x的时间,而下半部分花费y的时间。

我不认为以上内容允许我这样做,只是想知道哪种方法更好。

肯定需要使用

UIView animateWithDuration:animations: completion:

感谢您的协助。

这段代码会产生这种效果,

罗希特

UIImageView上的动画系统非常有限。我建议您使用2个图像视图进行自己的实现。

然后,您将使用UIView animateWithDuration在一个imageview中更改图像并进行淡入和淡出。

我已经为您编写了一种方法。请注意:我尚未测试。

假设您将框架放置在称为“框架”的数组中,并且两个UIIMageView相互叠放,分别称为“ imgv1”和“ imgv2”

-(void)performAnimationOfFrameAtIndex:(NSNumber*)indexNum
{        
int index = [indexNum intValue];
if(index >= frames.count)
{
    return;
}
UIImage *img = [frames objectAtIndex:index];

UIImageView *imgIn;
UIImageView *imgOut;
if(index % 2 == 0)
{
    imgIn = imgv1;
    imgOut = imgv2;
}
else 
{
    imgIn = imgv2;
    imgOut = imgv1;
}
imgIn.image = img;
[self.view sendSubviewToBack:imgIn];

[UIView animateWithDuration:0.1 animations:^
{
    imgIn.alpha = 1;
    imgOut.alpha = 0;
} completion:^(BOOL finished)
{
    [self performSelectorOnMainThread:@selector(performAnimationOfFrameAtIndex:) withObject:[NSNumber numberWithInt:index+1] waitUntilDone:NO];
}];

}

谢谢你的主意

我在上面更改了您的代码,以获得所需的效果,现在它可以完美运行。即使持续时间为0秒,alpha也会引起“频闪”效果。

这是我的最终代码

享受100 :-)

- (void) performAnimationOfFrameAtIndex:(NSNumber*)indexNum
{
    int index = indexNum.intValue;
    if (index >= self.frames.count) return;

    UIImage *img = self.frames[index];

    UIImageView *imgIn;
    UIImageView *imgOut;

    float speed = (index <= self.frames.count / 2) ? 0.1 : 1; // first half : second half speed.

    if (index % 2 == 0) {
        imgIn = self.animationImageViewOne;
        imgOut = self.animationImageViewTwo;
    }
    else {
        imgIn = self.animationImageViewTwo;
        imgOut = self.animationImageViewOne;
    }

    imgIn.image = img;

    [self.view sendSubviewToBack:imgIn];
    [self performSelector:@selector(performAnimationOfFrameAtIndex:) withObject:[NSNumber numberWithInt:index+1]  afterDelay:speed];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章