动画与CABasicAnimation并发的暗叠加

时间:2016-05-17 22:20:14

标签: ios swift calayer quartz-graphics caanimation

我试图重新创建样板动画:

UIView.transitionFromView(aView, toView: bView, duration: 1.0, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)

在动画中添加透视效果时,会翻转视图并添加暗叠加效果。我已经弄清楚如何使用CABasicAnimation翻转动画但是无法添加完美的叠加层。

我现在已经在互联网上浏览了一段时间,并尝试了一些事情,包括在动画之前添加叠加UIView,以及降低图层的不透明度,这是不起作用的。

这是我到目前为止所拥有的

点按手势:

func handleTap(){
    if currentAnimation == 0 {
        animateLayerAcrossDiagonalAxis(imageView.layer, r: CGFloat(M_PI / 2), x: 0, y: 1, z: 0, m34: 1.0 / (imageView.layer.frame.size.height - 10), keyPath: "transform", removedOnCompletion: false)
        }
    }

我的动画需要运行两次,所以我为正面或背面指定了不同的图像:

func toggleFront(){
    if front == true {
        front = false
    }
    else {
        front = true
    }

}

这是我的功能,我想创建黑暗叠加

func animateLayerAcrossDiagonalAxis(layer :CALayer, r: CGFloat, x: CGFloat, y: CGFloat, z: CGFloat, m34:CGFloat, keyPath:String, removedOnCompletion rOC:Bool){
    // store the layer's original values
    let original = layer.transform
    //transform based on set matrix
    let transform = CABasicAnimation(keyPath: "transform")
    transform.delegate = self
    transform.duration = 2.0
    transform.fromValue = original as? AnyObject
    // so we have KVC upon completion
    transform.removedOnCompletion = rOC
    layer.transform = CATransform3DConcat(layer.transform, CATransform3DRotate(CATransform3DIdentity, r, x, y, z))
    layer.transform.m34 = m34
    //m34 is the attribute of the matrix that sets perception
    layer.addAnimation(transform, forKey: keyPath)
}

在这里我再次调用该函数来完全翻转图像

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
    // makes sure the animation only flips twice
    if   anim == imageView.layer.animationForKey("transform") {
        // toggles the front and back image
        if front == true {
            imageView.image = UIImage(named: "2")
        }
        else {
            imageView.image = UIImage(named: "1")
        }
      toggleFront()
animateLayerAcrossDiagonalAxis(imageView.layer, r: -CGFloat(M_PI / 2), x: 0, y: 1, z: 0, m34: 1.0 / (imageView.layer.frame.size.height - 10), keyPath: "transformBack", removedOnCompletion: false)
    }
}

0 个答案:

没有答案