Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.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 为什么CATTransaction setCompletionBlock在动画持续时间之后延迟_Ios_Animation_Swift_Core Animation - Fatal编程技术网

Ios 为什么CATTransaction setCompletionBlock在动画持续时间之后延迟

Ios 为什么CATTransaction setCompletionBlock在动画持续时间之后延迟,ios,animation,swift,core-animation,Ios,Animation,Swift,Core Animation,我正在尝试使用CAShapeLayer练习CATTransaction和Cabasicanization,我得到了我想要的完美动画作品,但是动画完成后没有立即调用动画完成块 密码 以下是我使用的关键代码: func startAnimation(){ CATransaction.begin() CATransaction.setAnimationDuration(10.0) let pathAnimation = CABasicAnimation(keyPath: "st

我正在尝试使用CAShapeLayer练习CATTransaction和Cabasicanization,我得到了我想要的完美动画作品,但是动画完成后没有立即调用动画完成块

密码 以下是我使用的关键代码:

func startAnimation(){
    CATransaction.begin()
    CATransaction.setAnimationDuration(10.0)
    let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
    pathAnimation.duration = 10.0
    pathAnimation.fromValue = 0.0 as NSNumber
    pathAnimation.toValue = 1.0 as NSNumber

    CATransaction.setCompletionBlock { () -> Void in
        NSLog("finished")
    }
    pathLayer.addAnimation(pathAnimation, forKey: "strokeEnd")
    self.layer.addSublayer(pathLayer)
    CATransaction.commit()
    NSLog("start")
}
输出 NS2日志的输出如下所示:

2015-01-14 00:30:05.549 QuartzCorePlayground[47369:2769473] start
2015-01-14 00:30:22.004 QuartzCorePlayground[47369:2769473] finished
问题得到了答案 为什么动画完成后6.5秒左右总是打印完成?我的代码哪里错了

答复 原因是,正如评论中提到的,当我将代码放入实际项目中时,它看起来像是Xcode游乐场中的一个bug,正如我对CoreAnimation的理解一样

完整播放地代码 这是我在Xcode Playground中运行的完整代码。您需要检查Xcode中的Run in full Simulator选项

// Playground - noun: a place where people can play

import UIKit
import XCPlayground

func radians(degree : CGFloat) -> CGFloat{
    return CGFloat((Double(degree) / 180) * M_PI)
}

class TimerShapeView : UIView {

    var pathLayer = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let innerSize : CGFloat = 20
        var squareRectSize = min(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))
        var squareRect = CGRectInset(self.bounds, (CGRectGetWidth(self.bounds) - squareRectSize)/2 + innerSize, (CGRectGetHeight(self.bounds) - squareRectSize)/2 + innerSize)

        let workoutPath = UIBezierPath()
        workoutPath.addArcWithCenter(CGPoint(x: CGRectGetMidX(squareRect), y: CGRectGetMidY(squareRect)), radius: CGRectGetWidth(squareRect)/2, startAngle: radians(90), endAngle: radians(360), clockwise: true)

        pathLayer.frame = self.bounds
        pathLayer.path = workoutPath.CGPath
        pathLayer.lineWidth = 2.0
        pathLayer.strokeColor = UIColor.purpleColor().CGColor
        pathLayer.geometryFlipped = true
        pathLayer.fillColor = nil
        pathLayer.lineJoin = kCALineJoinBevel

        self.layer.addSublayer(pathLayer)

        self.backgroundColor = UIColor.greenColor().colorWithAlphaComponent(0.1)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func startAnimation(){
        CATransaction.begin()
        CATransaction.setAnimationDuration(10.0)
        let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
        pathAnimation.duration = 10.0
        pathAnimation.fromValue = 0.0 as NSNumber
        pathAnimation.toValue = 1.0 as NSNumber

        CATransaction.setCompletionBlock { () -> Void in
            NSLog("finished")
        }
        pathLayer.addAnimation(pathAnimation, forKey: "strokeEnd")
        self.layer.addSublayer(pathLayer)
        CATransaction.commit()
        NSLog("start")
    }

}

var shapeTimerView = TimerShapeView(frame: CGRect(x: 0, y: 0, width: 320, height: 220))
XCPShowView("newTimerView", shapeTimerView)

shapeTimerView.startAnimation()

除了操场,你有没有在实际项目中尝试过?我不会在项目内部受到延误。@rakeshbs你说得对。我可以确认这是一只游乐场的虫子。当我把代码放到实际项目中时,它工作得很好。谢谢