2011-06-01 59 views
0

如果我点击按钮,然后这个按钮是动画旋转,并再次添加self.view,但我想当这个按钮添加到视图超过10自己。鉴于然后删除第一个按钮,一个动画没有停止,我想在另一个线程删除按钮,但在动画停止动画同时从超级视图中删除按钮

我尝试另一个线程的代码,但不能正常工作没有效果,动画停止

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


} 

回答

0

这很不清楚你想要实现什么,但UIKit更新必须在主线程中完成。这应该解决它在另一个线程上不起作用的部分。

+0

我的问题是,只有我想从超视图中删除对象,但那时动画不会影响其他对象 – 2011-06-02 05:00:40