【问题标题】:Animate circular progress with Swift使用 Swift 动画循环进度
【发布时间】:2014-07-22 16:49:04
【问题描述】:

当用户单击此自定义控件时,我正在尝试使用 UIBezierPath 为弧设置动画。贝塞尔路径只是向前捕捉,没有任何动画。

我覆盖drawrect的原因是因为我想在界面生成器中显示这个自定义控件。有没有一种好的方法可以在界面生成器中显示循环进度并让进度动画化?

这是我的代码:

import Foundation
import UIKit
import QuartzCore

@IBDesignable class CircleControl:UIControl {
    @IBInspectable var progressTotal:CGFloat = 100

    @IBInspectable var progressCounter:CGFloat = 20
    {
    willSet(newProgressCounter) {
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.duration = 0.5;
        animation.toValue = newProgressCounter;
        animation.delegate = self;
        layer.addAnimation(animation, forKey: "strokeEnd")
    }

    didSet {
        setNeedsDisplay()
    }
    }

    override func drawRect(rect: CGRect) {
        let center = CGPointMake(bounds.size.width / 2, bounds.size.height / 2)
        let radius = bounds.size.width / 2;
        let pi = CGFloat(M_PI)
        let sliceAngle:CGFloat = (2 * pi) / progressTotal

        let progressAngle:CGFloat = sliceAngle * progressCounter
        let startAngle:CGFloat = CGFloat(-M_PI_2) + sliceAngle
        let endAngle:CGFloat = startAngle + progressAngle;

        let path = UIBezierPath()
        path.addArcWithCenter(center, radius: radius - 5, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        path.lineWidth = 5
        path.stroke()
    }

    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        println("touch")

        self.progressCounter = 80
    }

    override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
        self.progressCounter = 20
    }
}

【问题讨论】:

  • 你想在 IB 里面做动画?

标签: ios swift cabasicanimation


【解决方案1】:

您不能在 IB 内部制作动画。您也许可以在操场上执行此操作,但 IB 仅用于布局 - 它不执行动画。您必须在模拟器或游乐场中进行预览。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2020-05-08
    相关资源
    最近更新 更多