2011-06-01 97 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