首页 文章

如何暂停和恢复动画 - KDCircularProgress圆形条

提问于
浏览
0

我修改了一些从https://github.com/kaandedeoglu/KDCircularProgress拉出来的代码

我有一个小的UIView代表圆进度计时器 . 我已经引用它并将其实例化为 KDCircularProgress 类 .

我已设法实现启动和重置计时器进度循环条的方法 .

但是当我动画时,我遇到了麻烦 restarting 循环进度条 .

我的代码序言:

import UIKit

class SomeTableViewController: UITableViewController {

//Circular progress vars
var currentCount = 1.0
let maxCount = 60.0

//Reference selected UIView & instantiate
@IBOutlet weak var circularProgressView: KDCircularProgress!

开始动画 - 60秒动画:

if currentCount != maxCount {
    currentCount += 1
    circularProgressView.animateToAngle(360, duration: 60, completion: nil)
    }

要停止并重置动画:

currentCount = 0
circularProgressView.animateFromAngle(circularProgressView.angle, toAngle: 0, duration: 0.5, completion: nil)

要暂停动画:

circularProgressView.pauseAnimation()

如何在暂停状态后设置重新启动动画的方法?

非常感谢任何澄清 . 这是我的第一个动画,我试图自己解决这个问题,但似乎找不到适用于我的特定情况的任何语法 .

UPDATED SOLUTION:

感谢@Duncan C让我走上了正确的道路 .

我解决了我的问题如下......

因为我使用 currentCount += 1 启动了计数器的进度,我想我会尝试暂停计数器:

if currentCount != maxCount {
currentCount -= 1
circularProgressView.animateToAngle(360, duration: 60, completion: nil)
}

我认为这会对柜台产生'net'效果(净关闭 counter +=1counter -=1 )以有效地停止计数器 . 从理论上讲,这应该是反击的进展为零,但它继续倒计时 .

所以我回到 circularProgressView.pauseAnimation() 来暂停圆形计数器动画 .

要在暂停后重新启动动画,我必须修改持续时间以表示 updated 持续时间 - 即动画暂停的时间 .

我在这里使用了一些技巧并包含了一个NSTimer - 无论如何我碰巧在我的代码中 .

要在暂停时重新启动动画:

if currentCount != maxCount {
        currentCount += 1

        circularProgressView.animateToAngle(360, duration: NSTimeInterval(swiftCounter), completion: nil)
    }

我无法弄清楚如何更新动画圆形条的持续时间,但确实知道如何使用具有相同持续时间和倒计时速度的NSTimer计时器更新时间 . 所以我标记了对更新的计时器值的引用 . 问题解决了 ;)

My Code:

import UIKit

class SomeFunkyTableViewController: UITableViewController {

//Circular progress variables
var currentCount = 0.0
let maxCount = 60.0

@IBOutlet weak var circularProgressView: KDCircularProgress!


//Timer countdown vars
var swiftTimer = NSTimer()
var swiftCounter = 60



@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var timerView: UIView!

@IBOutlet weak var timerLabel: UILabel!

@IBOutlet weak var startView: UIView!

override func viewDidLoad() {

    circularProgressView.angle = 0

    timerLabel.text = String(swiftCounter)


        super.viewDidLoad()


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func startButton(sender: AnyObject) {


    pauseBtn.alpha = 1.0

    playBtn.alpha = 1.0

    stopBtn.alpha = 1.0

    circularProgressView.hidden = false


    if currentCount != maxCount {
        currentCount += 1

        circularProgressView.animateToAngle(360, duration: 60, completion: nil)
    }

    startView.hidden = true
    timerView.hidden = false


    swiftTimer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: #selector(SomeFunkyTableViewController.updateCounter), userInfo: nil, repeats: true)



}


@IBOutlet weak var pauseBtn: UIButton!
@IBAction func pauseButton(sender: AnyObject) {

  circularProgressView.pauseAnimation()



    swiftTimer.invalidate()

    pauseBtn.alpha = 0.5

    playBtn.alpha = 1.0

    stopBtn.alpha = 1.0
}


@IBOutlet weak var playBtn: UIButton!
@IBAction func playButton(sender: AnyObject) {




    if currentCount != maxCount {
        currentCount += 1

        circularProgressView.animateToAngle(360, duration: NSTimeInterval(swiftCounter), completion: nil)
    }




    if !swiftTimer.valid {
        swiftTimer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: #selector(SomeFunkyTableViewController.updateCounter), userInfo: nil, repeats: true)
    }


    if swiftCounter == 0 {
        swiftTimer.invalidate()
    }


    pauseBtn.alpha = 1.0

    playBtn.alpha = 0.5

    stopBtn.alpha = 1.0

}


@IBOutlet weak var stopBtn: UIButton!
@IBAction func stopButton(sender: AnyObject) {

    currentCount = 0
    circularProgressView.animateFromAngle(circularProgressView.angle, toAngle: 0, duration: 0.5, completion: nil)

    circularProgressView.hidden = true

    timerView.hidden = true
    startView.hidden = false



    swiftTimer.invalidate()
    swiftCounter = 60
    timerLabel.text = String(swiftCounter)


    pauseBtn.alpha = 1.0

    playBtn.alpha = 1.0

    stopBtn.alpha = 0.5
}

func updateCounter() {

    swiftCounter -= 1
    timerLabel.text = String(swiftCounter)

    if swiftCounter == 0 {
        swiftTimer.invalidate()
    }
}


}

侧注:我有两个重叠的视图 - StartView和TimerView . 一个隐藏在视图加载上,因此隐藏/取消隐藏引用 . 我按下按钮 - 播放/暂停/停止 .

2 回答

  • 3

    请注意,从iOS 10开始,有一种更新,更好的方法可以暂停和恢复 UIView 动画: UIViewPropertyAnimator .

    我在Github上有一个示例项目(用Swift编写)来演示这个新的 UIViewPropertyAnimator 类 . 这是链接:UIViewPropertyAnimator-test

    以下是该项目的README摘录:

    UIViewPropertyAnimator 允许您轻松创建可以暂停,反转和来回擦除的基于UIView的动画 .

    UIViewPropertyAnimator 对象采用动画块,非常类似于旧的 animate(withDuration:animations:)UIView 类方法 . 但是, UIViewPropertyAnimator 可用于运行多个动画块 .

    通过在动画制作工具上设置 fractionComplete 属性,可以内置支持擦除动画 . 但是,没有一种自动机制来观察动画进度 .

    您可以通过设置 isReversed 属性来反转 UIViewPropertyAnimator 动画,但有一些怪癖 . 如果将正在运行的动画师的 isReversed 属性从false更改为true,则动画会反转,但在动画运行时无法将 isReversed 属性从true设置为false,并将其从反向切换为正向"live" . 您必须先暂停动画,切换 isReversed 标志,然后重新启动动画 . (要使用汽车类比,您可以在移动时从正向切换到反向,但在从反向切换回驱动器之前必须先进行完全停止 . )

  • 0

    暂停和恢复动画需要特殊代码 . 如果不修改正在使用的库,则可能无法执行此操作 .

    诀窍是将托管动画的父图层上的动画速度设置为0,并记录动画的时间偏移 .

    以下是我的一个暂停和恢复动画的项目中的一些方法(用Objective-C编写):

    - (void) pauseLayer: (CALayer *) theLayer
    {
      CFTimeInterval mediaTime = CACurrentMediaTime();
      CFTimeInterval pausedTime = [theLayer convertTime: mediaTime fromLayer: nil];
      theLayer.speed = 0.0;
      theLayer.timeOffset = pausedTime;
    }
    
    //-----------------------------------------------------------------------------
    
    - (void) removePauseForLayer: (CALayer *) theLayer;
    {
      theLayer.speed = 1.0;
      theLayer.timeOffset = 0.0;
      theLayer.beginTime = 0.0;
    }
    
    //-----------------------------------------------------------------------------
    
    - (void) resumeLayer: (CALayer *) theLayer;
    {
      CFTimeInterval pausedTime = [theLayer timeOffset];
      [self removePauseForLayer: theLayer];
      CFTimeInterval mediaTime = CACurrentMediaTime();
      CFTimeInterval timeSincePause = 
        [theLayer convertTime: mediaTime fromLayer: nil] - pausedTime;
      theLayer.beginTime = timeSincePause;
    }
    

相关问题