一切的動(dòng)畫其實(shí)都是假象,3D效果也是這樣.本篇我們來(lái)做一個(gè)這樣的特效.
先忽略掉3D效果,我們先要做的是一個(gè)右拉的抽屜效果.
總體思路:
1.創(chuàng)建一個(gè)ContainerViewController容器控制器,然后把左側(cè)選擇菜單的SideMenuViewController,和右側(cè)負(fù)責(zé)顯示內(nèi)容的MainViewController 添加到ContainerViewController中.
2.給容器控制器ContainerViewController添加一個(gè)手勢(shì)監(jiān)聽(tīng),通過(guò)修改偏移量完成抽屜效果.
3.設(shè)置anchorPoint,給左側(cè)SideMenuViewController的view以及左上角的圖片按鈕添加3D效果.
如果想知道詳情請(qǐng)看代碼,我們這邊把核心的部分說(shuō)下:
- 給ContainerViewController添加手勢(shì)監(jiān)聽(tīng),然后在監(jiān)聽(tīng)方法中該去怎么處理:
func handleGesture(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translationInView(recognizer.view!.superview!)
// (isOpening ? 1.0 : -1.0) isOpening為BOOL值,表示打開或者關(guān)閉
var progress = translation.x / menuWidth * (isOpening ? 1.0 : -1.0)
// 保證在0~1之間
progress = min(max(progress, 0), 1.0)
switch recognizer.state {
case .Began:
let isOpen = mainVC.view.frame.origin.x/menuWidth
isOpening = isOpen == 1.0 ? false:true
// 為layer的效果添加一個(gè)緩存,作用是避免旋轉(zhuǎn)的時(shí)候產(chǎn)生鋸齒
sideMenuVC.view.layer.shouldRasterize = true;
// 設(shè)置渲染的范圍
sideMenuVC.view.layer.rasterizationScale = UIScreen.mainScreen().scale
case .Changed:
// 核心代碼:調(diào)整容器中視圖的位置 3D效果 以及透明度 以及左上角按鈕的翻轉(zhuǎn)效果
setToPercent(isOpening ? progress: (1.0 - progress))
case .Ended: fallthrough
case .Cancelled: fallthrough
case .Failed:
// 分頁(yè)效果
var targetProgress: CGFloat
if (isOpening) {
targetProgress = progress < 0.5 ? 0.0 : 1.0
}else {
targetProgress = progress < 0.5 ? 1.0 : 0.0
}
// 設(shè)置failed狀態(tài)下的sideMenuVC的view的效果
UIView.animateWithDuration(animationTime, animations: { () -> Void in
self.setToPercent(targetProgress)
}, completion: { (_) -> Void in
// 記得關(guān)閉layer的緩存渲染
self.sideMenuVC.view.layer.shouldRasterize = false
})
default: break
}
}
- setToPercent方法中的設(shè)置:
func setToPercent(percent: CGFloat) {
// 調(diào)整mainVC.view的位置
mainVC.view.frame.origin.x = menuWidth * CGFloat(percent)
// 設(shè)置3D效果及透明度
sideMenuVC.view.layer.transform = menuTransformForPercent(percent)
sideMenuVC.view.alpha = CGFloat(max(0.2, percent))
// 左上角按鈕的翻轉(zhuǎn)設(shè)置
let mainVc = (mainVC as! UINavigationController).viewControllers.first as? MainViewController
if let menuButton = mainVc?.menuButton {
menuButton.imageView.layer.transform = buttonTransformForPercent(percent)
}
}
- 3D效果的核心代碼:
// 根據(jù)百分比添加一個(gè)3D特效
func menuTransformForPercent(percent: CGFloat) -> CATransform3D {
var identify = CATransform3DIdentity
// m34負(fù)責(zé)z軸方向的translation(移動(dòng)),m34= -1/D, 默認(rèn)值是0, D越小透視效果越明顯,這邊的1000視情況調(diào)整的
identify.m34 = -1.0/1000
let remainingPercent = 1.0 - percent
let angle = remainingPercent * CGFloat(-M_PI_2)
// 后面3個(gè)數(shù)為 x y z
let rotationTransform = CATransform3DRotate(identify, angle, 0.0, 1.0, 0.0)
// 將值轉(zhuǎn)換成一個(gè)矩陣
let translationTransform =
CATransform3DMakeTranslation(menuWidth * percent, 0, 0)
// 將上面兩者結(jié)合起來(lái)
return CATransform3DConcat(rotationTransform,
translationTransform)
}
// 為按鈕添加一個(gè)3D效果
func buttonTransformForPercent(percent: CGFloat) -> CATransform3D {
var identity = CATransform3DIdentity
identity.m34 = -1.0/1000
let angle = percent * CGFloat(-M_PI)
let rotationTransform = CATransform3DRotate(identity, angle, 1.0, 1.0, 0.0)
return rotationTransform
}
本文整理自 : iOS.Animations.by.Tutorials.v2.0
源碼 : https://github.com/DarielChen/DemoCode
如有疑問(wèn),歡迎留言 :-D