开发者

Stop animation when same time remove button from superview

if i tap on button then this button is animate with rotation and add once again in self.view, but i want to when this button is add in view more than 10 in self.view then remove first button, and next animation is not stop, i want to remove button in another thread, but not effect on animation

I Try that code for another thread, but not work properly, animation is stop

-(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];
        theGrou开发者_Python百科p.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];


}


It's pretty unclear what you're trying to achieve but UIKit updates must be done in the main thread. This should address the part where it doesn't work on another thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜