升级到 Swift 3.0 后从左到右的动画不起作用

Left to right animation not working after upgrade to Swift 3.0

所以我刚从 2.3 升级到 Swift 3.0。除了这行代码及其错误:

,我项目的其他一切都很好
        if let delegate: AnyObject = completionDelegate {
        leftToRightTransition.delegate = delegate //ERROR MESSAGE
    }

错误消息说:

Cannot assign value of type 'AnyObject' to type CAAnimation

下面是完整的相关代码块。本质上,这段代码就是动画。在我的项目的其他地方(我肯定这与问题无关,但只是为了上下文)有代码允许用户滑动图像数组。此块是它的动画部分('swipe away' 动画'):

extension UIView {
func rightToLeftAnimation(_ duration: TimeInterval = 0.5, completionDelegate: AnyObject? = nil) {
    // Create a CATransition object
    let rightToLeftTransition = CATransition()

    // Set its callback delegate to the completionDelegate that was provided
    if let delegate: AnyObject = completionDelegate {
        rightToLeftTransition.delegate = delegate //ERROR MESSAGE ON THIS LINE
    }

    rightToLeftTransition.type = kCATransitionPush
    rightToLeftTransition.subtype = kCATransitionFromLeft
    rightToLeftTransition.duration = duration
    rightToLeftTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    rightToLeftTransition.fillMode = kCAFillModeRemoved

    // Add the animation to the View's layer
    self.layer.add(rightToLeftTransition, forKey: "rightToLeftTransition")
}

这在 Swift 2.3 中工作得很好,但现在不工作了。像这样的小事让我发疯...任何帮助将不胜感激;)

现在是 CAAnimationDelegate:

if let delegate = completionDelegate as? CAAnimationDelegate {
    rightToLeftTransition.delegate = delegate
}

或者只是:

func rightToLeftAnimation(_ duration: TimeInterval = 0.5, completionDelegate: CAAnimationDelegate? = nil) {
    // Create a CATransition object
    let rightToLeftTransition = CATransition()

    // Set its callback delegate to the completionDelegate that was provided
    rightToLeftTransition.delegate = completionDelegate

    rightToLeftTransition.type = kCATransitionPush
    rightToLeftTransition.subtype = kCATransitionFromLeft
    rightToLeftTransition.duration = duration
    rightToLeftTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    rightToLeftTransition.fillMode = kCAFillModeRemoved

    // Add the animation to the View's layer
    layer.add(rightToLeftTransition, forKey: "rightToLeftTransition")
}
Try this , Its working in my code:

 let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipedRight))
        swipeRight.direction = .right
        SLIDEVIEW.addGestureRecognizer(swipeRight)


        let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipedLeft))
        swipeLeft.direction = .left
        SLIDEVIEW.addGestureRecognizer(swipeLeft)

func swipedRight()
    {
        print("swiped right")
        updateFrames(towards: "right")
    }

    func swipedLeft()
    {
        print("swiped left")
        updateFrames(towards: "left")
    }