自定義視圖切換動(dòng)畫
需要先了解下面的效果對應(yīng)的代碼
宏(系統(tǒng)里面直接可以調(diào)用的)
kCATransitionFade 交叉淡化過渡
kCATransitionMoveIn 新視圖移到舊視圖上面
kCATransitionPush 新視圖把舊視圖推出去
kCATransitionReveal 將舊視圖移開,顯示下面的新視圖
字符串(也是系統(tǒng)的颂龙,但是是字符串喜最,要加“”使用)
pageCurl 向上翻一頁
pageUnCurl 向下翻一頁
rippleEffect 滴水效果
suckEffect 收縮效果再扭,如一塊布被抽走
cube 立方體效果
oglFlip 上下翻轉(zhuǎn)效果
cameraIrisHollowOpen
cameraIrisHollowClose
動(dòng)畫方向
kCATransitionFromRight;
kCATransitionFromLeft(默認(rèn)值)
kCATransitionFromTop贮庞;
kCATransitionFromBottom
轉(zhuǎn)場動(dòng)畫類代碼
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"
///上下翻轉(zhuǎn)效果
case OglFlip = "oglFlip"
}
public enum TransitionDirection:String{
///從右邊
case FromRight = "fromRight"
///從左邊
case FromLeft = "fromLeft"
///從上邊
case FromTop = "fromTop"
///從下邊
case FromBottom = "fromBottom"
}
extension UIView{
/**
添加轉(zhuǎn)場動(dòng)畫
- parameter durection:動(dòng)畫時(shí)間
- parameter type:動(dòng)畫類型
- parameter direction:動(dòng)畫方向
*/
public func addTransitionAnimation(durection:Double,type: TransitionType,direction: TransitionDirection){
//創(chuàng)建動(dòng)畫對象
let animation = CATransition()
//設(shè)置動(dòng)畫時(shí)間
animation.duration = durection
//設(shè)置動(dòng)畫類型
animation.type = type.rawValue
//設(shè)置動(dòng)畫方向
animation.subtype = direction.rawValue
//添加動(dòng)畫
self.window?.layer.addAnimation(animation, forKey: nil)
}
}
應(yīng)用
- 創(chuàng)建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
}
}
- 創(chuàng)建第一個(gè)視圖控制器
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(){
//添加轉(zhuǎn)場動(dòng)畫(不使用封裝好的方法)
/*
//a.創(chuàng)建轉(zhuǎn)場動(dòng)畫對象
let animation = CATransition()
//b.設(shè)置動(dòng)畫轉(zhuǎn)場時(shí)間
animation.duration = 2
//c.設(shè)置動(dòng)畫類型
//"rippleEffect", kCATransitionFade, kCATransitionMoveIn......
animation.type = "rippleEffect"
//d.設(shè)置動(dòng)畫方向
animation.subtype = kCATransitionFromRight
//e.添加動(dòng)畫
//可以通過任何已經(jīng)顯示在界面上的視圖去拿到當(dāng)前應(yīng)用程序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)
}
}
- 創(chuàng)建需要跳轉(zhuǎn)的第二個(gè)視圖控制器
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)
}
}