我希望视图循环进度的持续时间仅持续1秒,但在我的应用中,它只持续约0.84秒。
的Gif:
在viewDidLoad中:
let shapeLayer = CAShapeLayer()
let center = view.center
let trackLayer = CAShapeLayer()
let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.lineWidth = 10
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapRound
view.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 10
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineCap = kCALineCapRound
shapeLayer.strokeEnd = 0
view.layer.addSublayer(shapeLayer)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
如果点击屏幕:
@objc private func handleTap() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
///Here is defined the duration
basicAnimation.duration = 1
basicAnimation.fillMode = kCAFillModeForwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "urSoBasic")
}
我也尝试过:
basicAnimation.duration = CFTimeInterval(1)
但它也不起作用。
答案 0 :(得分:2)
它必须持续1秒才可能在你看到它之前已经运行了0.15秒左右。 ViewDidLoad并不代表你看到它。尝试在CACurrentMediaTime()+ 2.0上设置beginTime并再次测量它。或者在视图中运行它确实出现了CACurrentMediaTime()的beginTime。
更新: 使用您的代码
import UIKit
class ViewController: UIViewController {
var shapeLayer = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
shapeLayer = CAShapeLayer()
let center = view.center
let trackLayer = CAShapeLayer()
let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.lineWidth = 10
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapRound
view.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 10
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineCap = kCALineCapRound
shapeLayer.strokeEnd = 0
view.layer.addSublayer(shapeLayer)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
}
@objc private func handleTap() {
//prints //the time when the animation start is 30830.886492681
print("the time when the animation start is \(CACurrentMediaTime())")
CATransaction.begin()
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
///Here is defined the duration
basicAnimation.duration = 1
basicAnimation.fillMode = kCAFillModeForwards
basicAnimation.isRemovedOnCompletion = false
//here i make sure it starts at the current time
basicAnimation.beginTime = CACurrentMediaTime()
CATransaction.setCompletionBlock {
//prints the current Time is 30831.895261542
print("the current Time is \(CACurrentMediaTime())")
}
shapeLayer.add(basicAnimation, forKey: "urSoBasic")
CATransaction.commit()
}
}
在我的例子中,它持续了一毫秒,尽管我的赌注是动画实际开始时的帧差异。请记录您的视频而不是使用视频,看它是否持续时间应该持续。如果不是我们缺少的其他代码。你还有其他可以阻止主线程的东西吗?