嗨,我在屏幕周围制作了大约 10 个 UIButton。当用户点击一个按钮时,按钮会放大,然后移动并缩小到屏幕上的某个点。
我正在使用一种方法来管理动画。我将 UIButton 的索引号传递给该方法,以便该方法可以在该按钮上执行其动画。动画效果很好,但是当我在点击前一个按钮的缩小和移动动画完成之前点击第二个按钮时。点击前一个按钮的 Scale down 动画停止,但其移动动画完成。
这是我的代码。
-(void)HidingAnimation:(int)BubbleIndex{
//Obtain Bubble to hide using index number passed to method
UIButton *BubbleToHide = [Bubbles objectAtIndex:BubbleIndex - 1];
NSLog(@"BubbletoHide = %@", BubbleToHide);
[self.view bringSubviewToFront:BubbleToHide];
//Remove animations that are happening on the bubble
[BubbleToHide.layer removeAllAnimations];
//Set the position of the bubble to the current presentation layer position
CALayer *currentLayer = [BubbleToHide.layer presentationLayer];
BubbleToHide.layer.position = currentLayer.position;
//Remove the background image of the bubble
UIImage *buttonImage = [UIImage imageNamed:NULL];
[BubbleToHide setBackgroundImage:buttonImage forState:UIControlStateNormal];
//Find the position of the previously hidden bubble
UIButton *PreviousHiddenBubble = [BubblesArray objectAtIndex:BubbleIndex - 2];
CALayer *PreviousBubble = PreviousHiddenBubble.layer.presentationLayer;
PreviousHiddenBubble.layer.transform = PreviousBubble.transform;
//Calculate scaledown and position of bubble depending on game mode
CGFloat XTranslation, YTranslation, ScaleDownX, ScaleDownY;
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"Mode"]isEqualToString:@"game"]) {
XTranslation = 44 + ((PreviousHiddenBubble.bounds.size.width/2) * (BubbleIndex -1))-10; //PreviousHiddenBubble.center.x + PreviousHiddenBubble.bounds.size.width*.35;
YTranslation = 702;
ScaleDownX = 0.5;
ScaleDownY = 0.5;
}
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationCurveLinear|UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowAnimatedContent
animations:^{
BubbleToHide.layer.transform = CATransform3DMakeScale(4, 4, 1);
}
completion:^(BOOL finished) {
NSLog(@"Scale Up Animation Complete");
[UIView animateWithDuration:1.2
delay:0
options:UIViewAnimationCurveLinear|UIViewAnimationOptionOverrideInheritedDuration
animations:^{
BubbleToHide.transform = CGAffineTransformMakeScale(ScaleDownX, ScaleDownY);//CATransform3DMakeScale
BubbleToHide.center = CGPointMake(XTranslation, YTranslation);
}
completion:^(BOOL finished){
NSLog(@"Scale down and Transform Complete");
}];
}];
}
我把 ScaleDown 和 move 动画放在了完成的 scale up 动画中。
BubbleToHide 变量应该持有对使用传递的索引从 Bubbles 数组中检索到的每个 UIButton 的单独引用。
我可以通过代码顶部附近的 NSLog 来确认这一点,但是每当点击第二个 UIButton 并且在前一个点击的按钮上没有完成 scaleDown 时。之前的按钮 scaleDown 被取消。然而,移动动画完成了,所以没有得到一排漂亮的按钮。我有一个位于正确位置的按钮,其大小固定在 scaleUp 因子和 scaleDown 因子之间。取决于点击下一个按钮的速度。
任何想法为什么会发生这种情况?或修复?
谢谢。