How do I simultaneously animate a UIImageView's image and the UIImageView itself?

后端 未结 4 1959
余生分开走
余生分开走 2020-12-16 08:26

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 animationI

4条回答
  •  情书的邮戳
    2020-12-16 08:51

    This is not a good way to make a sprite. I could insist that you should be using OpenGL, but there may be no need to go that far; you can do it all with Core Animation of a layer, and I think you'll be better off doing that than trying to use a full-fledged UIImageView.

    Using Core Animation of a layer, I was able to make this PacMan sprite animate across the screen while opening and closing his mouth; isn't that the sort of thing you had in mind?

    enter image description here

    Here is a video showing the animation!

    http://www.youtube.com/watch?v=WXCLc9ww8MI

    And yet the actual code creating the animation is extremely simple: just two layer animations (Core Animation), one for the changing image, the other for the position.

    You should not award this answer the bounty! I am now merely echoing what Seamus Campbell said; I'm just filling out the details of his answer a little.

    Okay, so here's the code that generates the movie linked above:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // construct arr, an array of CGImage (omitted)
        // it happens I've got 5 images, the last being the same as the first
        self.images = [arr copy];
        // place sprite into the interface
        self.sprite = [CALayer new];
        self.sprite.frame = CGRectMake(30,30,24,24);
        self.sprite.contentsScale = [UIScreen mainScreen].scale;
        [self.view.layer addSublayer:self.sprite];
        self.sprite.contents = self.images[0];
    }
    
    - (void)animate {
        CAKeyframeAnimation* anim = 
            [CAKeyframeAnimation animationWithKeyPath:@"contents"];
        anim.values = self.images;
        anim.keyTimes = @[@0,@0.25,@0.5,@0.75,@1];
        anim.calculationMode = kCAAnimationDiscrete;
        anim.duration = 1.5;
        anim.repeatCount = HUGE_VALF;
    
        CABasicAnimation* anim2 = 
            [CABasicAnimation animationWithKeyPath:@"position"];
        anim2.duration = 10;
        anim2.toValue = [NSValue valueWithCGPoint: CGPointMake(350,30)];
    
        CAAnimationGroup* group = [CAAnimationGroup animation];
        group.animations = @[anim, anim2];
        group.duration = 10;
    
        [self.sprite addAnimation:group forKey:nil];
    }
    

提交回复
热议问题