自定义视图切换动画
需要先了解下面的效果对应的代码
宏(系统里面直接可以调用的)
kCATransitionFade 交叉淡化过渡
kCATransitionMoveIn 新视图移到旧视图上面
kCATransitionPush 新视图把旧视图推出去
kCATransitionReveal 将旧视图移开,显示下面的新视图
字符串(也是系统的,但是是字符串,要加“”使用)
pageCurl 向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect 收缩效果,如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果
cameraIrisHollowOpen
cameraIrisHollowClose
动画方向
kCATransitionFromRight;
kCATransitionFromLeft(默认值)
kCATransitionFromTop;
kCATransitionFromBottom
转场动画类代码
import UIKit
public enum TransitionType:String{
///交叉淡化过度
case Fade = "fade"
///新视图移到就是图上边
case MoveIn = "moveIn"
///新视图把旧视图推出去
case Push = "push"
///将旧视图移开,显示下面的新视图
case Reveal = "reveal"
///向上翻一页
case PageCurl = "pageCurl"
///向下翻一页
case PageUnCurl = "pageUnCurl"
///滴水效果
case RippleEffect = "rippleEffect"
///收缩效果,如一块布被抽走
case SuckEffect = "suckEffect"
///立方体效果
case Cube = "cube"
///上下翻转效果
case OglFlip = "oglFlip"
}
public enum TransitionDirection:String{
///从右边
case FromRight = "fromRight"
///从左边
case FromLeft = "fromLeft"
///从上边
case FromTop = "fromTop"
///从下边
case FromBottom = "fromBottom"
}
extension UIView{
/**
添加转场动画
- parameter durection:动画时间
- parameter type:动画类型
- parameter direction:动画方向
*/
public func addTransitionAnimation(durection:Double,type: TransitionType,direction: TransitionDirection){
//创建动画对象
let animation = CATransition()
//设置动画时间
animation.duration = durection
//设置动画类型
animation.type = type.rawValue
//设置动画方向
animation.subtype = direction.rawValue
//添加动画
self.window?.layer.addAnimation(animation, forKey: nil)
}
}
应用
- 创建window
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let root = FirstViewController()
window?.rootViewController = root
return true
}
}
- 创建第一个视图控制器
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.greenColor()
let btn = UIButton(frame: CGRectMake(100,100,100,50))
btn.setTitle("下一页", forState: .Normal)
self.view.addSubview(btn)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchUpInside)
// Do any additional setup after loading the view.
}
}
extension FirstViewController{
func btnAction(){
//添加转场动画(不使用封装好的方法)
/*
//a.创建转场动画对象
let animation = CATransition()
//b.设置动画转场时间
animation.duration = 2
//c.设置动画类型
//"rippleEffect", kCATransitionFade, kCATransitionMoveIn......
animation.type = "rippleEffect"
//d.设置动画方向
animation.subtype = kCATransitionFromRight
//e.添加动画
//可以通过任何已经显示在界面上的视图去拿到当前应用程序window(主窗口)
self.view.window?.layer.addAnimation(animation, forKey: nil)
*/
//使用自己封装的方法
self.view.addTransitionAnimation(0.2, type: TransitionType.Push, direction: TransitionDirection.FromTop)
let second = SecondViewController()
presentViewController(second, animated: true, completion: nil)
}
}
- 创建需要跳转的第二个视图控制器
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.orangeColor()
let btn = UIButton(frame: CGRectMake(100,100,100,50))
btn.setTitle("上一页", forState: .Normal)
self.view.addSubview(btn)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchUpInside)
}
}
extension SecondViewController{
func btnAction() {
//返回上一页
self.dismissViewControllerAnimated(true, completion: nil)
}
}