gpt4 book ai didi

iphone - 制作平滑的动画进入、退出和交换两个 ImageView

转载 作者:搜寻专家 更新时间:2023-10-30 20:27:19 28 4
gpt4 key购买 nike

我正在使用 UIView 的动画功能将一张鱼的图像游到屏幕上,当屏幕中间已经有一条鱼时,将它游走并游上一条新鱼。鱼的选择是从一个单独的 TableView Controller 发送的。

为了实现这一点,我保留了 UIImageView 的两个实例变量,并在动画完成后将 self.fishViewCurrent 交换为 self.fishViewNext(参见下面的代码)。

我的问题是当用户在下一条鱼有机会到达之前快速按下新的表格单元格时。我的快速解决方案是延迟将添加鱼的新消息,此时什么也不做。这适用于 2 次快速触摸,但不能更多。如果有人知道我如何改进下面的代码,我正在寻找想法,以便动画流畅,您可以单击任何内容,而最终显示的图像是最后一次单击的图像。以下是重要的方法:

创建第一个 ImageView :

- (void) createFish {

self.fishViewCurrent = [[[UIImageView alloc] init] autorelease];
self.fishViewCurrent.contentMode = UIViewContentModeScaleAspectFit;
self.fishViewCurrent.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight);
self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // off the screen to the left
[self addSubview:self.fishViewCurrent];

}

选择表格 View 单元格时调用的公共(public)方法:

- (void) addFish:(UIImage *)fish {

if (self.fishViewNext) {
[self performSelector:@selector(addFish:) withObject:(UIImage *)fish afterDelay:1.0];
return;
}
self.fishViewNext = [[[UIImageView alloc] initWithImage:fish] autorelease];
self.fishViewNext.contentMode = UIViewContentModeScaleAspectFit;
self.fishViewNext.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight);
self.fishViewNext.center = CGPointMake(self.bounds.size.width + kFishWidth, kFishYPos); // off the screen right
[self addSubview:self.fishViewNext];

[UIView beginAnimations:@"swimFish" context:nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fishSwamOffHandler:finished:context:)];
[UIView setAnimationRepeatAutoreverses:NO];

self.fishViewNext.center = CGPointMake(self.center.x, kFishYPos); // center
self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // exit stage left

[UIView commitAnimations];
}

将“下一条”鱼(UIImageView)切换到“当前”的处理程序:

- (void)fishSwamOffHandler:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

[self.fishViewCurrent removeFromSuperview];
self.fishViewCurrent = self.fishViewNext;
self.fishViewNext = nil;
}

最佳答案

最简单的事情是创建一个 ivar BOOL,比如 _fishIsSwimming,然后创建一个 _queuedFishView,然后将 addFish: 的开头更改为如下所示:

- (void) addFish:(UIImage *)fish 
{
// queue the most recent fish, if one was passed in
if(fish)
self.queuedFishView = [[[UIImageView alloc] initWithImage:fish] autorelease];

// If something is swimming, leave now
if(_fishIsSwimming)
return;

// If we get here and there's no queued fish, leave now
if(!self.queuedFishView)
return;

// We have a queued fish and nothing is swimming, so do the queued fish next
// and clear the queued fish
_fishIsSwimming = YES;
self.nextFishView = self.queuedFishView;
self.queuedFishView = nil;
// Then carry on as normal
}

然后在你的动画完成回调中,你这样做:

-(void) fishSwamOffHandler:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context 
{
// We're done animating, so ready to do another swimming fish
_fishIsSwimming = NO;

[self.fishViewCurrent removeFromSuperview];
self.fishViewCurrent = self.fishViewNext;
self.fishViewNext = nil;

// now call to add another fish with nil, so that it will use
// the queued fish if there is one and otherwise just early out
[self addFish:nil];
}

所以会发生的是,您将始终在 queuedFishView 中维护最近传入的鱼,然后一旦动画结束,您就可以回到 addFish,如果有鱼等着去,那么您发送。如果不是,您只需等待按下按钮发送的下一个

关于iphone - 制作平滑的动画进入、退出和交换两个 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4171729/

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