gpt4 book ai didi

ios - 移动时为多个帧设置动画的正确方法

转载 作者:行者123 更新时间:2023-12-01 19:12:20 25 4
gpt4 key购买 nike

我想知道这是否是在游戏角色从 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
}];
}];
}];

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

最佳答案

似乎有更好的方法。如果您没有太多图像,我可能会建议使用 animationImages UIImageView 的属性(property)然后 startAnimating :

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

如果您担心将所有这些加载到内存中,您也可以使用 CADisplayLink根据需要加载图像,如下所示:
- (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;
}
}

关于ios - 移动时为多个帧设置动画的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com