Preface
Recently, due to work needs, I plan to use swift to refactor existing projects. During the implementation process, I found that many system animation implementations were used, because the previous ones were implemented using OC, so here we mainly implement some animation effects through swift.
This article mainly implements different effects of CATransition animation.
Step 1 Create a swift file and declare the corresponding enumeration type
enum TransitionAnimType : Int { case fade = 0, //Fake in and outpush, //Pushingreveal, //OpenmoveIn, //covercube, //cubesuckEffect, //suckoglFlip, //FliprippleEffect, //ripplepageCurl, //Flip pagepageUnCurl, //Reverse pagecameraIrisHollowOpen, //Open the lenscameraIrisHollowClose, //Close the lenscurlDown, //Flip the page nextcurlUp, //Flip pageflipFromLeft, //Flip leftflipFromRight, //Flip rightramdom //random} enum TransitionSubType : Int { case top = 0, //superiorleft, //Leftbottom, //Downright, //rightramdom //random} enum TransitionCurve : Int { case Default = 0, //defaultEaseIn, //Slowly move forwardEaseOut, //Slow outEaseInEaseOut, //Slow in and outLinear, //LinearRamdom //random}
The above three enum types represent:
- TransitionAnimType: Animation Type
- TransitionSubType: Animation Direction
- TransitionCurve: Animated curves
Step 2 Custom function returns animation type
/// Return to the animation typeprivate func animationType(animType: TransitionAnimType) -> String { /// Set the transition animation type let animTypeArray = ["fade", "push", "reveal", "moveIn", "cube", "suckEffect", "oglFlip", "rippleEffect", "pageCurl", "pageUnCurl", "cameraIrisHollowOpen", "cameraIrisHollowClose", "curlDown", "curlUp", "flipFromLeft", "flipFromRight", "ramdom"] return objectFromDataSource(array: animTypeArray, index: , isRamdom: ( == animType)) as! String }
Step 3 Custom function returns animation direction
/// Return to the animation directionprivate func animationSubType(subType: TransitionSubType) -> String { let animSubTypeArray = [kCATransitionFromTop, kCATransitionFromLeft, kCATransitionFromBottom, kCATransitionFromRight] return objectFromDataSource(array: animSubTypeArray, index: , isRamdom: ( == subType)) as! String }
Step 4: Custom function returns animation curve
/// Return to the animation curveprivate func animationCurve(curve: TransitionCurve) -> String { let animCurveArray = [kCAMediaTimingFunctionDefault, kCAMediaTimingFunctionEaseIn, kCAMediaTimingFunctionEaseOut, kCAMediaTimingFunctionEaseInEaseOut, kCAMediaTimingFunctionLinear] return objectFromDataSource(array: animCurveArray, index: , isRamdom: ( == curve)) as! String }
Step 5 It is not difficult to find that we have used the objectFromDataSource method among the above three custom methods. It is not difficult to find out from our parameter transmission. It is to return the specified data we need. Let’s implement this method below.
/// Unified return object from dataprivate func objectFromDataSource(array: Array<Any>, index: Int, isRamdom: Bool) -> AnyObject { let count = let i = isRamdom ? Int(arc4random_uniform(UInt32(count))) : index return array[i] as AnyObject }
Step 6 Okay, now all the preparations are done. Next, let’s take a look at the specific animation implementation method.
func layerTransition(animTye: TransitionAnimType, subType: TransitionSubType, curve: TransitionCurve, duration: CGFloat, layer: CALayer) { let key = "transition" if (forKey: key) != nil { (forKey: key) } let transition = CATransition() /// Anime duration = CFTimeInterval(duration) /// Animation type = animationType(animType: animTye) /// Animation direction = animationSubType(subType: subType) /// Easing function = CAMediaTimingFunction(name: animationCurve(curve: curve)) /// Complete animation deletion = true (transition, forKey: key) }
The work is done! Next, we just need to use animation to retrieve the method we implemented in step 6. so easy! Ha ha
layerTransition(animTye: .ramdom, subType: .ramdom, curve: .Ramdom, duration: 2.0, layer: (?.layer)!)
Doesn't it feel very simple? You can use the above code by combining it and use it directly. If you need it, you won't thank you.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.