Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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 Swift,当核心动画完成时_Ios_Swift_Swift2_Core Animation - Fatal编程技术网

Ios Swift,当核心动画完成时

Ios Swift,当核心动画完成时,ios,swift,swift2,core-animation,Ios,Swift,Swift2,Core Animation,我目前有一个Swift应用程序,可以将正方形的填充颜色从红色更改为绿色。我想做的是,当动画完成后,我想再执行一些代码 这是我的档案: import UIKit class PathViewController: TapToCloseViewController { override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let tempView = UIV

我目前有一个Swift应用程序,可以将正方形的填充颜色从红色更改为绿色。我想做的是,当动画完成后,我想再执行一些代码

这是我的档案:

import UIKit

class PathViewController: TapToCloseViewController {

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let tempView = UIView(frame: CGRectMake(0,0,100,100))
        tempView.layer.borderColor = UIColor.blackColor().CGColor
        tempView.layer.borderWidth = 2
        view.addSubview(tempView)

        let bounds = CGRect(x: 0, y: 0, width: 100, height: 100)

        // Create CAShapeLayerS
        let rectShape = CAShapeLayer()
        rectShape.bounds = bounds
        rectShape.position = tempView.center
        rectShape.cornerRadius = bounds.width / 2
        tempView.layer.addSublayer(rectShape)
        tempView.backgroundColor = UIColor.redColor()

        // Apply effects here

        // fill with white
        rectShape.fillColor = UIColor.greenColor().CGColor

        // 1
        // begin with a circle with a 50 points radius
        //let startShape = UIBezierPath(roundedRect: bounds, cornerRadius: 50).CGPath
        let sRect = CGRect(x:0, y:0, width: 0, height: bounds.height)
        let bpath = UIBezierPath(rect: sRect).CGPath

        // animation end with a large circle with 500 points radius
        let drect = CGRect(x:0, y:0, width: bounds.width, height: bounds.height)
        let endShape = UIBezierPath(rect: drect).CGPath

        //let endShape = UIBezierPath(roundedRect: CGRect(x: -450, y: -450, width: 1000, height: 1000), cornerRadius: 500).CGPath

        // set initial shape
        rectShape.path = bpath

        // 2
        // animate the `path`
        let animation = CABasicAnimation(keyPath: "path")
        animation.toValue = endShape
        animation.duration = 1 // duration is 1 sec
        // 3
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) // animation curve is Ease Out 
        animation.fillMode = kCAFillModeBoth // keep to value after finishing
        animation.removedOnCompletion = false // don't remove after finishing
        // 4 
        rectShape.addAnimation(animation, forKey: animation.keyPath)

    }

}

如何检测动画何时停止?

要设置颜色更改动画,请使用此选项

  UIView.animateWithDuration(0.3, animations: { () -> Void in
    aView.backgroundColor = UIColor.redColor()
    }) { (completed) -> Void in
      //animation has completed
      //execute your code here
  }

要设置颜色更改的动画,请使用以下命令

  UIView.animateWithDuration(0.3, animations: { () -> Void in
    aView.backgroundColor = UIColor.redColor()
    }) { (completed) -> Void in
      //animation has completed
      //execute your code here
  }

您可以将自己设置为动画的代理:

let animation = CABasicAnimation(keyPath: "path")
.....
animation.delegate = self
然后执行以下方法:

override func animationDidStop(anim: CAAnimation, finished flag: Bool)
{
    NSLog("My beautiful animation has finished.")
}

您可以将自己设置为动画的代理:

let animation = CABasicAnimation(keyPath: "path")
.....
animation.delegate = self
然后执行以下方法:

override func animationDidStop(anim: CAAnimation, finished flag: Bool)
{
    NSLog("My beautiful animation has finished.")
}

很明显,颜色变化发生的地方。很明显,颜色变化发生的地方。在你给出这个答案之前,我已经编辑了我的代码。这对我来说确实有效,但是我用@YakivKovalskiy编辑了我的代码answer@iProgram不管怎样,我很高兴它最终对你有效。谢谢,我仍然支持你的答案,因为它有效。在你给出这个答案之前,我已经编辑了我的代码。这对我来说确实有效,但是我用@YakivKovalskiy编辑了我的代码answer@iProgram不管怎样,我很高兴它最终对你有效。谢谢,我仍然支持你的答案,因为它有效。