【问题标题】:iOS: Increase animation by click a buttoniOS:通过单击按钮增加动画
【发布时间】:2018-11-09 11:11:21
【问题描述】:

我有以下动画,我需要实现一个代码,通过点击一个按钮动画增加一,直到我想要达到它的值。

我尝试以下代码:

    let strokeIt = CABasicAnimation(keyPath: "strokeEnd")
    let timeLeftShapeLayer = CAShapeLayer()
    let bgShapeLayer = CAShapeLayer()



 override func viewDidLoad() {
        drawBgShape()
        drawTimeLeftShape()
      }


func drawBgShape() {
    bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
        100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
    bgShapeLayer.strokeColor = UIColor.white.cgColor
    bgShapeLayer.fillColor = UIColor.clear.cgColor
    bgShapeLayer.lineWidth = 15
    view.layer.addSublayer(bgShapeLayer)
}

func drawTimeLeftShape() {
    timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
        100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
    timeLeftShapeLayer.strokeColor = UIColor.red.cgColor
    timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
    timeLeftShapeLayer.lineWidth = 15
    view.layer.addSublayer(timeLeftShapeLayer)
}

当点击按钮时:

     var x = 0.0
    @IBAction func click(_ sender: Any) {

        strokeIt.fromValue = x
        strokeIt.toValue = x + 0.1

        strokeIt.duration = 0.25
        timeLeftShapeLayer.add(strokeIt, forKey: nil)
        x = x + 0.1
}

我怎样才能做到这一点?

【问题讨论】:

    标签: ios animation swift4 cabasicanimation


    【解决方案1】:

    您不需要使用显式动画来实现这一点。

    对于CALayer 的某些属性,Core Animation 会为你添加一个隐式动画strokeEnd 属性就是其中之一。

    这意味着 Core Animation 将自动为层的strokeEnd 属性的任何更改设置动画。您无需创建动画对象并自行添加。

    您需要对实现进行一些修改才能使其正常工作。

    1) 为了看起来像您发布的图片,背景圆圈应该在您的drawBgShape 函数中包含一个灰色的strokeColor

    bgShapeLayer.strokeColor = UIColor.lightGray.cgColor
    

    2) timeLeftShapeLayerstrokeEnd 应设置为 0.0 作为其在 drawTimeLeftShape 函数中的起始值。

     timeLeftShapeLayer.strokeEnd = 0.0
    

    3) 从你的类中移除 strokeIt 动画属性,你不需要它。

    4) 更新您的 click 函数以直接缓慢增加 timeLeftShapeLayerstrokeEnd 属性。这是一行简单的代码:

    timeLeftShapeLayer.strokeEnd += 0.1
    

    这是一个示例实现:

    class ViewController: UIViewController {
    
        let timeLeftShapeLayer = CAShapeLayer()
        let bgShapeLayer = CAShapeLayer()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            drawBgShape()
            drawTimeLeftShape()
        }
    
        func drawBgShape() {
            bgShapeLayer.path = UIBezierPath(
                arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY),
                radius: 100,
                startAngle: -90.degreesToRadians,
                endAngle: 270.degreesToRadians,
                clockwise: true
            ).cgPath
            bgShapeLayer.strokeColor = UIColor.lightGray.cgColor
            bgShapeLayer.fillColor = UIColor.clear.cgColor
            bgShapeLayer.lineWidth = 15
            view.layer.addSublayer(bgShapeLayer)
        }
    
        func drawTimeLeftShape() {
            timeLeftShapeLayer.path = UIBezierPath(
                arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY),
                radius: 100,
                startAngle: -90.degreesToRadians,
                endAngle: 270.degreesToRadians,
                clockwise: true
            ).cgPath
            timeLeftShapeLayer.strokeColor = UIColor.red.cgColor
            timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
            timeLeftShapeLayer.lineWidth = 15
            timeLeftShapeLayer.strokeEnd = 0.0
            view.layer.addSublayer(timeLeftShapeLayer)
        }
    
        @IBAction func click(_ sender: Any) {
            timeLeftShapeLayer.strokeEnd += 0.1
        }
    
    
    }
    

    【讨论】:

    • 非常感谢,已经成功了,但是如何设置动画的最大值和最小值?
    • 不客气。不确定你的意思 - 你是在谈论 from 和 to 价值观吗?如果您可以修改您的问题以解释您正在寻找的功能,这将有助于理解。
    • 谢谢,我达到了我所需要的。我想设置停止值(最大点击次数)@IBAction func click(_ sender: Any) { timeLeftShapeLayer.strokeEnd += 1/34 if timeLeftShapeLayer.strokeEnd == 1 { timeLeftShapeLayer.strokeEnd = 0.0 } }
    猜你喜欢
    • 2020-06-05
    • 2021-09-26
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2015-07-02
    相关资源
    最近更新 更多