简体   繁体   English

更改动画速度系列图像

[英]Change Animation Speed Series of Images

I have a series of images, I want them to repeat. 我有一系列图像,我想让它们重复一遍。 However, I want the first half to play in, for example 2 seconds, then the second half to play in 5 seconds. 但是,我希望上半场比赛进行,例如2秒,然后下半场比赛将在5秒内进行。

It is quite easy to play a series of images, using the following 使用以下内容可以很容易地播放一系列图像

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

However, I want the first half of the animation to take x time and the second half to take y time. 但是,我希望动画的前半部分占用x时间,而后半部分需要y时间。

I don't think the above will allow me to do this, just wondering what a better approach would be. 我不认为上面的内容会让我这样做,只是想知道什么是更好的方法。

Pretty sure will need to use 非常肯定需要使用

UIView animateWithDuration:animations: completion:

Thanks for any assistance. 谢谢你的帮助。

This code creates this effect, 此代码创建此效果,

The animation system on UIImageView is very limited. UIImageView上的动画系统非常有限。 I would suggest that you make your own implementation with say 2 image view. 我建议您使用2图像视图制作自己的实现。

You would than change the image in one imageview and the fade in and out using UIView animateWithDuration 您可以使用UIView animateWithDuration更改一个imageview中的图像和淡入淡出

I have written a method for you. 我已经为你写了一个方法。 Please note: I have not tested it. 请注意:我还没有测试过。

It assumes you have your frames in a array called 'frames' and have two UIIMageView placed on top of each other called 'imgv1' and 'imgv2' 它假设你的帧在一个名为'frames'的数组中,并且有两个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];
}];

}

Thanks for the idea, 谢谢你的想法,

I changed your code above to get the desired effect I wanted, now it works perfect. 我改变了你上面的代码以获得我想要的效果,现在它完美无缺。 The alpha was causing a "strobe" effect even when the duration is 0 seconds. 即使持续时间为0秒,alpha也会产生“频闪”效果。

Here is my final code 这是我的最终代码

Enjoy the 100 :-) 享受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];
}

I don't think the above will allow me to do this, just wondering what a better approach would be. 我不认为上面的内容会让我这样做,只是想知道什么是更好的方法。

Actually, it will. 实际上,它会。 You just need to repeat the images as you form the sequence. 您只需在形成序列时重复图像。 For the images in the first group, repeat each one twice before going on to the next one. 对于第一组中的图像,重复每一次,然后再进行下一次。 For the images in the second group, repeat each one five times before going on to the next one. 对于第二组中的图像,重复每个图像五次,然后再进行下一个图像。 Now set the time for the total animation to 7 seconds. 现在将动画总时间设置为7秒。 Since there are about the same number of images in each group originally, you can readily see that this means the first group will take about 2 seconds and the second group will take about 5 seconds. 由于最初每组中的图像数量大致相同,因此您可以很容易地看到这意味着第一组将花费大约2秒钟,第二组将花费大约5秒钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM