【问题标题】:How to pause and resume UIView Animation (without Block Animation)?如何暂停和恢复 UIView 动画(没有块动画)?
【发布时间】:2012-02-01 22:42:31
【问题描述】:

如何暂停和恢复 UIView 动画? (没有块动画)

我很难弄清楚这一点,所以下面是我关于如何做到这一点的来源。

【问题讨论】:

    标签: cocoa-touch uiview uiviewanimation


    【解决方案1】:

    如何暂停和恢复 UIView 动画:

    这将介绍如何在没有块动画的情况下执行上述操作。 (人们仍然希望支持 3.0 及更高版本)。

    这仅允许在动画到达设置位置后暂停。对于如何在动画中间暂停动画,我建议使用 CALayers:

    CALayer* myLayer = [self.myUIView.layer presentationLayer];
    CGRect frameStop = myLayer.frame;
    double pausedX = frameStop.origin.x;
    double pausedY = frameStop.origin.y;
    

    现在是实际如何:

    startAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        startAnimation.titleLabel.font = [UIFont systemFontOfSize:22];
        startAnimation.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation;
        [startAnimation setTitle:(@"pause") forState:UIControlStateNormal];
        [startAnimation addTarget:self action:@selector(doAnimation) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:startAnimation];
    
    
    
    -(void)doAnimation{
        if (bicyclePad.animationPause == false){
           [restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal];
           [bicyclePad pauseAnimation];
        } else {
           [restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal];
           [bicyclePad resumeAnimation];
        }
    }
    
    -(void)resumeAnimation{
        bicyclePad.animationPause = false;
        [restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal];
        objectMotion = [[yourUIView alloc]initWithFrame:CGRectMake((*yourPathPoints)[0].x, (*yourPathPoints)[0].y, 8, 8)];
        [self.view addSubview:objectMotion];
        [self animateBike:nil finished:YES context:nil];
    
    
    }
    
    -(void)pauseAnimation{
       bicyclePad.animationPause = true;
    
       [restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal];
       [bicyclePad doAnimation];
    }
    
    -(void)animateObject:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationBeginsFromCurrentState:YES];
        //below suggesting your animation loops
        if (animationPause == true)
           [UIView setAnimationDidStopSelector:@selector(animateStop:finished:context:)];
        else
        [UIView setAnimationDidStopSelector:@selector(animateObject:finished:context:)];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationCurve: UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:yourDelay];
        [UIView commitAnimations];
        animationPause == false;
    
    }
    
    -(void)animateStop:(NSString*)animationID finished:(BOOL)finished context:(void*)context{
    }
    

    【讨论】:

    • animationPause == false;animationPause == true; 是比较,不做任何事情。您可能想改为分配值。
    • 恐怕你没有。您将一个比较替换为另一个比较。 bicyclePad.animationPause == false; 仍然是一个比较,结果被扔掉了。你想要bicyclePad.animationPause = false;。注意===
    • 啊好吧。我想念你的第一条评论。是的,这段代码大约有 2 年的历史了。感谢您指出了这一点。 @DarkDust
    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2012-04-17
    • 2015-07-06
    相关资源
    最近更新 更多