为啥要更改选项卡,停止 ios 应用程序中的动画?

Posted

技术标签:

【中文标题】为啥要更改选项卡,停止 ios 应用程序中的动画?【英文标题】:why changing of tab, stopping the animation in ios app?为什么要更改选项卡,停止 ios 应用程序中的动画? 【发布时间】:2014-09-08 11:38:23 【问题描述】:

我的应用中有多个标签。我一个视图控制器正在运行动画, 当我切换到另一个视图控制器时,再次进入带有动画的视图控制器,然后动画停止,

任何人都可以指导我使用我的 Xcode,甚至在 Iphone 应用程序中切换选项卡吗?

- (IBAction)btn:(id)sender 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *soundStatus = [defaults objectForKey:@"Sound_Swich"];
    //NSLog(@"soundstatus is %@ ", soundStatus);
    if([soundStatus isEqual:@"YES"])
    
        [self soundEffect];
    
    if ([btnpress isEqualToString:@"start"]) 

        btnpress= @"pause";

        int radius = 100;
        circle = [CAShapeLayer layer];

        circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                                 cornerRadius:radius].CGPath;

        circle.fillColor = [UIColor clearColor].CGColor;
        circle.strokeColor = [UIColor greenColor].CGColor;

        circle.lineWidth = 5;
        [imgview.layer addSublayer:circle];



        // Configure animation
         drawAnimation= [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        drawAnimation.duration            = 5.0; // "animate over 10 seconds or so.."
        drawAnimation.repeatCount         = 8.0;  // Animate only once..
        // Animate from no part of the stroke being drawn to the entire stroke being drawn
        drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

        // Experiment with timing to get the appearence to look the way you want
        drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

        // Add the animation to the circle
        [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
        timerWasStarted=YES;
        //totalSeconds = 120;
        totalSeconds = 10;
        twoMinTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                       target:self
                                                     selector:@selector(timer)
                                                     userInfo:nil
                                                      repeats:YES];

       // NSLog(@"start %@",[NSDate date]);
    // to pause animation and timer
    else if ([btnpress isEqualToString:@"pause"])
        flag=1;
        btnpress=@"resume";
        pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
        previousFireDate = [twoMinTimer fireDate];
        [twoMinTimer setFireDate:[NSDate distantFuture]];
       // NSLog(@"pause %@",[NSDate date]);
        [self pauseLayer:circle];


    // to resume animation and timer

    else if([btnpress isEqual:@"resume"]&&flag==1)
        btnpress= @"pause";
        float pauseTime = -1*[pauseStart timeIntervalSinceNow];
        [twoMinTimer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
        [self resumeLayer:circle];
    

    - (void)pauseLayer:(CALayer *)layer
    

        pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
        layer.speed = 0.0;
        layer.timeOffset = pausedTime;
        //NSLog(@"pauseoffset %f",pausedTime);
    

    - (void)resumeLayer:(CALayer *)layer
    
       // NSLog(@"resume %@",[NSDate date]);

        CFTimeInterval pausedTime1 = [layer timeOffset];
        layer.speed = 1.0;
        layer.timeOffset = 0.0;
        layer.beginTime = 0.0;
        timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime1;
        layer.beginTime = timeSincePause;

        //NSLog(@"resume offset %f",[layer timeOffset]);
    

【问题讨论】:

viewWillAppear: 方法在动画标签中有没有? 即使在 viewWillAppear 中编写代码也会停止动画, 每当你改变选项卡并返回到同一个选项卡时,viewWillAppear: 方法将被调用并且视图恢复到正常形式。 但是动画变量没有保持那里的值,如果你得到泄漏点,请检查这个代码。 我正在尝试添加代码,但由于声誉较低我无法,对不起,你能给我声誉, 【参考方案1】:

我需要的答案是

 drawAnimation.removedOnCompletion=NO;

【讨论】:

以上是关于为啥要更改选项卡,停止 ios 应用程序中的动画?的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 中更改语言时选项卡消失

从模态视图控制器 ios7 更改选项卡栏应用程序的选定索引

为啥当我使用 .map 而不是硬编码来动态填充内容时,Material UI 选项卡停止工作?

以编程方式停止 iOS 中的应用升级

在链接上单击 (Ctrl + 单击) 时停止打开新选项卡而不更改 href 详细信息

使用tabview更改选项卡时如何使SwiftUI中的计时器保持触发