Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 进度周期持续时间与预期不符_Ios_Swift_Animation_Swift4_Duration - Fatal编程技术网

Ios 进度周期持续时间与预期不符

Ios 进度周期持续时间与预期不符,ios,swift,animation,swift4,duration,Ios,Swift,Animation,Swift4,Duration,我希望查看循环进度的持续时间精确到1秒,但在我的应用程序中,它仅持续约0.84秒 Gif: 在viewDidLoad中: let shapeLayer = CAShapeLayer() let center = view.center let trackLayer = CAShapeLayer() let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFlo

我希望查看循环进度的持续时间精确到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)

但它也不起作用。

它必须持续1秒,可能是在你看到它之前,它已经运行了0.15秒左右。ViewDidLoad并不意味着您可以看到它。尝试将起始时间设置为CACurrentMediaTime()+2.0,然后再次测量。或者在视图中运行此命令,但该视图的起始时间为CACurrentMediaTime()

更新: 使用您的代码

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()
    }


}

在我的示例中,它持续了一毫秒以上,尽管我打赌这是动画实际开始时的帧差。请记录你的,而不是使用视频,看看它是否持续的时间应该持续。如果没有,我们还缺少什么代码。您是否有其他东西可能会阻止主线程?

很抱歉,我不明白您在CACurrentMediaTime()+2.0上对beginTime的定义。
beginTime
是在
CamediaTime
协议中声明的属性,您可以为
基本信息
对象设置它,因为它符合协议。建议的值为当前时间+2秒,以便在动画以更清晰的时间开始时能够看到并直观地验证控件(2秒应该足够了,除非在显示视图之前很久加载视图)。如A-Live所述,animation.beginTime=caccurrentMediaTime()+0.1。这将起到延迟的作用,尽管它还有其他用途。只需知道viewDidLoad并不意味着它在屏幕上,因此该层可能会在您看到它之前设置动画。在VIEW中执行此操作更有意义可能是因为您正在执行的操作,只需将beginTime设置为CACurrentMediaTime(),这基本上类似于您所看到的屏幕时间0。是的,请为我认为您正在尝试执行的操作移动代码。在ViewDid中,似乎不需要延迟,因此只需将CACurrentMediaTime作为beginTime。更新代码以记录实际时间。如果对您来说有差异,那么可能存在我们没有看到的代码。