So I just upgraded to Swift 3.0 from 2.3. Everything else about my project is fine except for this line of code and its error:
if let delegate: AnyObject = completionDelegate {
leftToRightTransition.delegate = delegate //ERROR MESSAGE
}
The error message says:
Cannot assign value of type 'AnyObject' to type CAAnimation
Below is the full related block of code. Essentially this code is the animation. Elsewhere in my project (which I'm positive is not related to the problem, but just for context) there's code that allows the user to swipe through an array of images. This block is the animation part of it (the 'swipe away' animation'):
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")
}
This worked just fine in Swift 2.3 but now it doesn't work. It's little stuff like this that drives me nuts... Any help would be appreciated ;)
It's now a CAAnimationDelegate
:
if let delegate = completionDelegate as? CAAnimationDelegate {
rightToLeftTransition.delegate = delegate
}
Or just:
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")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.