[英]Stop animation when same time remove button from superview
如果我点击按钮然后这个按钮是动画旋转并在self.view中再次添加,但我想当这个按钮在self.view中添加超过10个时,然后删除第一个按钮,下一个animation不会停止,我想删除另一个线程中的按钮,但不会影响 animation
我为另一个线程尝试该代码,但无法正常工作,animation 已停止
-(void)putFruietAtOriginPositionInMainThread:(NSDictionary*)btnDictionary{
//if (!dragFlag) {
[self playSoundEffect:@"abc.mp3" withRepeatCount:0];
//[self.view bringSubviewToFront:btnBag];
//[self.view bringSubviewToFront:bagView];
UIButton *btnFruiet = [btnDictionary objectForKey:@"btnFruiet"];
int position= [[btnDictionary objectForKey:@"position"]intValue];
CGFloat xPosition = [[[dicCenterPoint objectForKey:[NSString stringWithFormat:@"%i",btnFruiet.tag]] objectAtIndex:0]floatValue];
CGFloat yPosition = [[[dicCenterPoint objectForKey:[NSString stringWithFormat:@"%i",btnFruiet.tag]] objectAtIndex:1]floatValue];
btnFruiet.center = self.view.center;
//btnFruiet.enabled = FALSE;
//[self playSoundEffect:@"shapes_collapse.mp3" withRepeatCount:0];
if ([dicFruitTimer count]>0) {
NSTimer *timer = [dicFruitTimer valueForKey:[NSString stringWithFormat:@"%i",btnFruiet.tag]];
if (timer) {
[timer invalidate];
timer=nil;
[dicFruitTimer removeObjectForKey:[NSString stringWithFormat:@"%i",btnFruiet.tag]];
}
}
NSTimer *fruitTimer = [NSTimer timerWithTimeInterval:0.00002 target:self selector:@selector(setFrameOfFruietWithTimer:) userInfo:[NSDictionary dictionaryWithObject:btnFruiet forKey:@"fruit"] repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:fruitTimer forMode:NSRunLoopCommonModes];
[dicFruitTimer setObject:fruitTimer forKey:[NSString stringWithFormat:@"%i",btnFruiet.tag]];
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:FRUIT_ANIMATION_DURATION/2] forKey:kCATransactionAnimationDuration];
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef positionPath = CGPathCreateMutable();
CGPathMoveToPoint(positionPath, NULL, [btnFruiet layer].position.x , [btnFruiet layer].position.y);
CGPathAddLineToPoint(positionPath, NULL, xPosition, yPosition);
positionAnimation.path = positionPath;
//CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// rotateAnimation.delegate = self;
// //rotateAnimation.autoreverses = YES;
// rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// rotateAnimation.fromValue = [NSNumber numberWithFloat:2.0];
// rotateAnimation.toValue = [NSNumber numberWithFloat:1.0];
// rotateAnimation.fillMode = kCAFillModeForwards;
// rotateAnimation.removedOnCompletion = NO;
CABasicAnimation *shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrinkAnimation.fromValue = [NSNumber numberWithFloat:kScaleFactor];
shrinkAnimation.toValue = [NSNumber numberWithFloat:1.0f];
CAAnimationGroup *theGroup = [CAAnimationGroup animation];
theGroup.delegate = self;
theGroup.removedOnCompletion = NO;
theGroup.fillMode = kCAFillModeForwards;
theGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
theGroup.animations = [NSArray arrayWithObjects:positionAnimation, shrinkAnimation,nil];
NSString *strPosition = [NSString stringWithFormat:@"%d,3", position];
[theGroup setValue:strPosition forKey:@"AnimationGroup"];
[[btnFruiet layer] addAnimation:theGroup forKey:@"AnimationGroup"];
//btnAnimationRunning = btnFruiet;
[CATransaction commit];
CGPathRelease(positionPath);
//[arrTimers removeObject:timer];
//}
if ([arrFruitOnScreen count]>10) {
//NSThread *thread = [[NSThread alloc] init];
//[self performSelector:@selector(removeFruit) withObject:nil];
[self performSelector:@selector(removeFruit) withObject:nil afterDelay:FRUIT_ANIMATION_DURATION/2];
//[thread start];
//[self performSelector:@selector(removeFruit) onThread:thread withObject:nil waitUntilDone:NO];
//}
}
-(void)removeFruit{
UIButton *btnF = [arrFruitOnScreen objectAtIndex:0];
[[btnF layer] removeAllAnimations];
[arrFruitOnScreen removeObjectAtIndex:0];
[btnF removeFromSuperview];
}
目前还不清楚您要实现什么,但 UIKit 更新必须在主线程中完成。 这应该解决它在另一个线程上不起作用的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.