1

我想知道这是否是在游戏角色从 a 点移动到 b 点时为游戏角色设置动画的正确方法,同时显示不同的运动图像来模拟一个人的运动。

这是我所做的伪代码:

    [UIView animateWithDuration:dMAN_MOVEMENT_SPEED/3 animations:^{
            //Set Target Image
            //Set target position
    }completion:^(BOOL finished){
        //2nd to 3rd Frame
        [UIView animateWithDuration:dMAN_MOVEMENT_SPEED/3 animations:^{
            //Set Target Image
            //Set target position
        }completion:^(BOOL finished){
            //3rd to final Frame
            [UIView animateWithDuration:dMAN_MOVEMENT_SPEED/3 animations:^{
                //Set Target Image
                //Set target position
            }completion:^(BOOL finished){
                //do nothing
            }];
        }];
    }];

这是正确的方法吗?我这样做是因为我可以控制每一帧和每一帧的持续时间。

4

1 回答 1

3

似乎有更好的方法。如果您没有太多图像,我可能会建议使用thenanimationImages的属性:UIImageViewstartAnimating

NSArray *images = @[[UIImage imageNamed:@"0.jpg"],
                    [UIImage imageNamed:@"1.jpg"],
                    [UIImage imageNamed:@"2.jpg"],
                    [UIImage imageNamed:@"3.jpg"],
                    [UIImage imageNamed:@"4.jpg"],
                    [UIImage imageNamed:@"5.jpg"],
                    [UIImage imageNamed:@"6.jpg"],
                    [UIImage imageNamed:@"7.jpg"]];

self.imageView.image = images[0];

self.imageView.animationImages = images;
self.imageView.animationDuration = kAnimationDuration;
self.imageView.animationRepeatCount = 1;

[self.imageView startAnimating];

[UIView animateWithDuration:kAnimationDuration
                 animations:^{
                     self.imageView.center = ... // set this to whatever you want
}];

如果您担心将所有这些加载到内存中,您还可以使用 aCADisplayLink来适当地加载图像,如下所示:

- (void)animateImageUsingDisplayLink
{
    self.imageNames = @[@"0.jpg",
                        @"1.jpg",
                        @"2.jpg",
                        @"3.jpg",
                        @"4.jpg",
                        @"5.jpg",
                        @"6.jpg",
                        @"7.jpg"];

    self.imageView.image = [UIImage imageNamed:self.imageNames[0]];

    [self startDisplayLink];

    [UIView animateWithDuration:kAnimationDuration
                     animations:^{
                         self.imageView.center = ... // set this to whatever you want
    }];
}

- (void)startDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    self.startTime = CACurrentMediaTime();
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
    CFTimeInterval elapsed = displayLink.timestamp - self.startTime;

    // If you want it to repeat, then comment out the following `if` statement

    if (elapsed >= kAnimationDuration)
    {
        [self stopDisplayLink];
        return;
    }

    NSInteger frameNumber = ((NSInteger) elapsed * [self.imageNames count] / kAnimationDuration) % [self.imageNames count];

    if (frameNumber != self.currentFrameNumber)
    {
        self.imageView.image = [UIImage imageNamed:self.imageNames[frameNumber]];
        self.currentFrameNumber = frameNumber;
    }
}
于 2013-03-26T04:35:30.903 回答