• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 以模态方式显示另一个 vc 时,CABasicAnimation 已完成

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:38:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用下面的方式来暂停/恢复动画

func pauseAnimation(){
  var pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
  layer.speed = 0.0
  layer.timeOffset = pausedTime
}

func resumeAnimation(){
  var pausedTime = layer.timeOffset
  layer.speed = 1.0
  layer.timeOffset = 0.0
  layer.beginTime = 0.0
  let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
  layer.beginTime = timeSincePause
}

只要当前呈现 ViewController,它就可以发挥魅力。

当我以模态方式呈现另一个 View Controller 然后将其关闭时,无论在此呈现/关闭操作期间经过了多少时间,动画都会完成。

您对为什么会发生这种情况有什么建议吗?如何解决?我想补充一点,所有其他 View 都保持其状态,只有动画完成。

编辑:

我刚刚发现无论暂停/恢复都会发生这种情况 - 在这种情况下,正在进行的动画也会完成。

这是我展示动画实现的代码

import Foundation

import UIKit

class CircleView: UIView {

var circleLayer: CAShapeLayer!

@IBOutlet var view: UIView!


@IBOutlet weak var progressLabel: UILabel!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    Bundle.main.loadNibNamed("CircleView", owner: self, options: nil)
    self.addSubview(view)
    view.frame = self.bounds

}

override func awakeFromNib() {
        super.awakeFromNib()
        setupAppearance()

}

func setupAppearance() {
    progressLabel.textColor = UIColor.textColor
    progressLabel.font = UIFont.textTimerClock
}


func setup(progressouble, clockwise:Bool) {

    self.backgroundColor = UIColor.clear
    var strokeColor = UIColor.positiveProgressColor
    if !clockwise { strokeColor = UIColor.positiveProgressColor }

    // Use UIBezierPath as an easy way to create the CGPath for the layer.
    // The path should be the entire circle.
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(.pi * 2 * progress), clockwise: clockwise)

    // Setup the CAShapeLayer with the path, colors, and line width
    circleLayer = CAShapeLayer()
    circleLayer.path = circlePath.cgPath
    circleLayer.fillColor = UIColor.clear.cgColor
    circleLayer.strokeColor = strokeColor.cgColor
    circleLayer.lineWidth = 8.0;

    // Don't draw the circle initially
    circleLayer.strokeEnd = 0.0

    // Add the circleLayer to the view's layer's sublayers
    layer.addSublayer(circleLayer)

    //add grey path
    let greyCircleLayer = CAShapeLayer()

    let greyCirclePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(.pi * 2.0), clockwise: true)

    greyCircleLayer.path = greyCirclePath.cgPath
    greyCircleLayer.fillColor = UIColor.clear.cgColor
    greyCircleLayer.strokeColor = UIColor.appLightGrey.cgColor
    greyCircleLayer.lineWidth = 1.0;

    // Don't draw the circle initially
    circleLayer.strokeEnd = 0.0

    // Add the circleLayer to the view's layer's sublayers
    layer.insertSublayer(greyCircleLayer, below: circleLayer)
    if progressLabel != nil {

        progressLabel.text = "10"}
}

func pauseAnimation(){
    let pausedTime = circleLayer.convertTime(CACurrentMediaTime(), from: nil)
    circleLayer.speed = 0.0
    circleLayer.timeOffset = pausedTime
}

func resumeAnimation(){
    let pausedTime = circleLayer.timeOffset
    circleLayer.speed = 1.0
    circleLayer.timeOffset = 0.0
    circleLayer.beginTime = 0.0
    let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
    circleLayer.beginTime = timeSincePause
}


func animateCircle(duration: TimeInterval, color: UIColor) {
    // We want to animate the strokeEnd property of the circleLayer
            circleLayer.strokeColor = color.cgColor
    let animation = CABasicAnimation(keyPath: "strokeEnd")

    // Set the animation duration appropriately
    animation.duration = duration

    // Animate from 0 (no circle) to 1 (full circle)
    animation.fromValue = 0
    animation.toValue = 1

    // Do a linear animation (i.e. the speed of the animation stays the same)
    animation.timingFunction = CAMediaTimingFunction(name: kCAAnimationLinear)

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the
    // right value when the animation ends.
    circleLayer.strokeEnd = 1.0

    // Do the actual animation
    circleLayer.add(animation, forKey: "animateCircle")
}
}

还有一点值得一提:init()、awakeFromNib() 没有被再次调用,所以情况并非如此。

另一个:推送 VC 而不是模态呈现也是如此。



Best Answer-推荐答案


通常,当您显示另一个 View Controller 时,当前 View Controller 的 View 会从窗口中移除。这也会从它们的图层中删除所有待处理的动画,并且任何现有的动画完成处理程序都会使用 false 调用,因为动画尚未完成(另见 https://stackoverflow.com/a/21200504/2352344 )。

返回 View Controller 后要继续动画,你应该在viewWillAppear中重建动画对象。

关于ios - 以模态方式显示另一个 vc 时,CABasicAnimation 已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46096086/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap