我如何同时动画一个UIImageView的形象和自身的UIImageView? [英] How do I simultaneously animate a UIImageView's image and the UIImageView itself?

查看:266
本文介绍了我如何同时动画一个UIImageView的形象和自身的UIImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能不是那么清楚,但我想要做的就是让UIImageView的展示一系列图片(有点像一个GIF),我通过设置 animationImages 来一个图像数组,然后调用 [ImageView的startAnimating]; 。这工作得很好。我也有code,与一个CABasicAnimation周围移动的UIImageView,而这部动画code也能正常工作。然而,当我尝试这两个动画的UIImageView的图像,并尝试四处移动的UIImageView,该停的UIImageView动画的图像。有没有解决办法?

The title may not be so clear, but what I want to do is to make the UIImageView display a series of images (sort of like a gif) and I do this by setting the animationImages to an array of images and then calling [imageView startAnimating];. This works fine. I also have code that moves the UIImageView around with a CABasicAnimation, and this animation code also works fine. However, when I try to both animate the images of the UIImageView and try to move the UIImageView around, the images of the UIImageView stop animating. Is there a workaround?

下面是我的code动画效果的的UIImageView的内容:

Here's my code for animating the content of the UIImageView:

    self.playerSprite = [[UIImageView alloc] initWithImage:[self.player.animationImages objectAtIndex:0]];
    [self.playerSprite setFrame:CGRectMake(self.center.x, self.center.y, self.tileSet.constants.TILE_WIDTH, self.tileSet.constants.TILE_HEIGHT)];
    self.playerSprite.animationImages = self.player.animationImages;
    self.playerSprite.animationDuration = self.tileSet.constants.animationDuration;
    self.playerSprite.animationRepeatCount = 0; //Makes it repeat indefinitely

这是我的一个CABasicAnimation动画的UIImageView的编码:

And here's my coding for animating the UIImageView with a CABasicAnimation:

float playerSpriteX = self.playerSprite.center.x;
float playerSpriteY = self.playerSprite.center.y;

CABasicAnimation *moveAnimation = [CABasicAnimation animation];
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(playerSpriteX + TILE_WIDTH, playerSpriteY)];
[moveAnimation setDelegate:self];
[moveAnimation setFillMode:kCAFillModeForwards];
[moveAnimation setRemovedOnCompletion:NO];
[moveAnimation setDuration:MOVE_ANIMATION_DURATION];       
[self.playerSprite.layer addAnimation:moveAnimation forKey:@"position"];

因此​​,尽管的UIImageView的地位正在被动画GIF的效果是行不通的。我的问题是的我怎样才能使它所以通过图像阵列的UIImageView的周期,同时它的位置被动画?

推荐答案

这是炒作,我没有测试过这种想法可言,但你有没有考虑推行动漫形象以同样的方式你是动画的位置,以 CAKeyframeAnimation ?你需要构建 CGImage 的数组从的UIImage 对象的数组(设置的关键帧动画的财产),但它看起来像pretty简单的转换:

This is speculation and I haven't tested this idea at all, but have you considered implementing the image animation in the same fashion you're animating the position, with a CAKeyframeAnimation? You'd need to construct an array of CGImage objects from your UIImage array (to set the values property of the keyframe animation) but it looks like a pretty straightforward conversion:

CAKeyframeAnimation *imageAnimation = [CAKeyframeAnimation animation];
imageAnimation.calculationMode = kCAAnimationDiscrete; // or maybe kCAAnimationPaced
imageAnimation.duration = self.tileSet.constants.animationDuration;
imageAnimation.repeatCount = HUGE_VALF;
// the following method will need to be implemented to cast your UIImage array to CGImages
imageAnimation.values = [self animationCGImagesArray];
[self.playerSprite.layer addAnimation:imageAnimation forKey:@"content"];

-(NSArray*)animationCGImagesArray {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self.player.animationImages count]];
    for (UIImage *image in self.player.animationImages) {
        [array addObject:(id)[image CGImage]];
    }
    return [NSArray arrayWithArray:array];
}

这篇关于我如何同时动画一个UIImageView的形象和自身的UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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