最近闲来无事,突然对微信, 微博, QQ等社交APP的九宫格的图片浏览功能非常感兴趣, 最近就尝试着研究了一下:
这里先附上Demo地址
- 在介绍项目之前, 先介绍三个基础知识
CATransition
转场动画
ViewController
自定义转场
UIBesization
贝塞尔曲线
CATransition转场动画
示例代码
1 2 3 4 5 6 7
| let transition = CATransition() transition.type = transitionType transition.subtype = isNext ? kCATransitionFromRight : kCATransitionFromLeft transition.duration = 1 downloadImage(url: imageURL) baseImage.layer.add(transition, forKey: "transition")
|
更多关于该动画的详细介绍, 可参考iOS出门必备之CoreAnimation(核心动画)中第七种CA动画, 故这里不多做介绍
ViewController
自定义转场
从iOS7开始,苹果更新了自定义ViewController转场的API,会用到的几个协议protocol
:
- 描述ViewController转场的:
UIViewControllerTransitioningDelegate
UINavigationControllerDelegate
UITabBarControllerDelegate
- 定义动画内容的
UIViewControllerAnimatedTransitioning
UIViewControllerInteractiveTransitioning
- 表示动画上下文的
UIViewControllerContextTransitioning
描述ViewController转场的
- 为什么苹果要引入这一套API?因为在iOS7之前,做转场动画很麻烦,要写一大堆代码在ViewController中。
- 引入这一套API之后,在丰富功能的同时极大程度地降低了代码耦合,实现方式就是将之前在ViewController里面的代码通过protocol分离了出来。
- 顺着这个思路往下想,实现自定义转场动画首先需要找到ViewController的delegate。
- 苹果告诉我们切换ViewController有三种形式:
UITabBarController
内部切换
UINavigationController
切换
present ViewController
- 这三种方式是不是需要不同的protocol呢?
详解Protocol
UIViewControllerTransitioningDelegate
自定义模态转场动画时使用。
- 设置
UIViewController
的属性transitioningDelegate
1
| weak open var transitioningDelegate: UIViewControllerTransitioningDelegate?
|
UINavigationControllerDelegate
自定义navigation转场动画时使用
- 设置
UINavigationController
的属性delegate
1
| weak open var delegate: UINavigationControllerDelegate?
|
UITabBarControllerDelegate
自定义tab转场动画时使用
- 设置
UITabBarController
的属性delegate
1
| weak open var delegate: UITabBarControllerDelegate?
|
实际上这三个protocol干的事情是一样的只不过他们的应用场景不同罢了。我们下面以UINavigationControllerDelegate为例,其他的类似
UINavigationControllerDelegate
主要的方法
1 2 3 4 5 6 7
| @available(iOS 7.0, *) optional public func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
@available(iOS 7.0, *) optional public func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
|
- 两个方法分别返回
UIViewControllerInteractiveTransitioning
和UIViewControllerAnimatedTransitioning
,它们的任务是描述动画行为(转场动画如何执行,就看它俩的)。
- 从名字可以看出,这两个
protocol
的区别在于是否是interactive
的。如何理解?
interactive
动画可以根据输入信息的变化改变动画的进程。例如iOS系统为UINavigationController
提供的默认右滑退出手势就是一个interactive
动画,整个动画的进程由用户手指的移动距离控制
UIViewControllerInteractiveTransitioning
协议
定义了两个属性可以做到平滑过渡
completionCurve
: 交互结束后剩余动画的速率曲线
completionSpeed
: 交互结束后动画的开始速率由该参数与原来的速率相乘得到,实际上是个缩放参数,这里应该使用单位变化速率(即你要的速率/距离)。
- 注意:
completionSpeed
会影响剩余的动画时间,而不是之前设定的转场动画时间剩下的时间;
- 当
completionSpeed
很小时剩余的动画时间可能会被拉伸得很长,所以过滤下较低的速率比较好。
- 如果不设置两个参数,转场动画将以原来的速率曲线在当前进度的速率继续。
- 不过从实际使用效果来看,往往不到0.5s的动画时间,基本上看不出什么效果来
定义动画内容的UIViewControllerAnimatedTransitioning
1 2 3 4 5 6
| public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
|
表示动画上下文UIViewControllerContextTransitioning
UIViewControllerContextTransitioning提供了一系列方法
- 是唯一一个我们不需要实现的Protocol
- 下面是一些我们常用的属性和方法:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public var containerView: UIView { get }
public func completeTransition(_ didComplete: Bool)
public func viewController(forKey key: UITransitionContextViewControllerKey) -> UIViewController?
@available(iOS 8.0, *) public func view(forKey key: UITransitionContextViewKey) -> UIView?
|
UIViewControllerTransitioningDelegate
自定义模态转场时使用
1 2 3 4 5 6 7 8 9 10 11 12
| func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresent = true return self }
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresent = false return self }
|
图片浏览器项目介绍
项目结构Alamofire + MVVM
自定义ViewController
的弹出和消失动画
自定义ViewController
弹出和消失的Protocol
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| protocol JunBrowsePresentDelefate: NSObjectProtocol { func imageForPresent(indexPath: IndexPath) -> UIImageView
func startImageRectForpresent(indexPath: IndexPath) -> CGRect
func endImageRectForpresent(indexPath: IndexPath) -> CGRect }
protocol JunBrowserDismissDelegate { func imageViewForDismiss() -> UIImageView
func indexPathForDismiss() -> IndexPath }
|
遵循协议
UIViewControllerTransitioningDelegate
告诉系统弹出/消失动画的处理页面
UIViewControllerAnimatedTransitioning
- 需要返回动画的执行时间
- 需要在弹出和消失页面的时候分别执行不同的动画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| extension PhotoBrowseAnimation: UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresent = true return self }
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresent = false return self } }
extension PhotoBrowseAnimation: UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.6 }
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext) } }
|
图片列表界面
在点击需要展示的imageView的时候,调用下面的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| extension JunScrollViewController { fileprivate func presentPhotoBrowse(indexPath: IndexPath) { let photoBrowseVC = BrowseViewController(images: imageVM.imageArray, currentIndexP: indexPath) photoBrowseVC.modalPresentationStyle = .custom photoBrowseVC.transitioningDelegate = photoAnimation photoAnimation.setProperty(indexPath: indexPath, self, photoBrowseVC) present(photoBrowseVC, animated: true, completion: nil) } }
|
遵循并实现自定义的协议方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| extension JunScrollViewController: JunBrowsePresentDelefate { func imageForPresent(indexPath: IndexPath) -> UIImageView { let imageV = UIImageView() imageV.contentMode = .scaleAspectFill imageV.clipsToBounds = true imageV.kf.setImage(with: URL(string: imageVM.imageArray[indexPath.item].pic74), placeholder: UIImage(named: "coderJun")) return imageV }
func startImageRectForpresent(indexPath: IndexPath) -> CGRect { guard let cell = imageCollection.cellForItem(at: indexPath) else { return CGRect(x: imageCollection.bounds.width * 0.5, y: kScreenHeight + 50, width: 0, height: 0) }
return imageCollection.convert( cell.frame, to: UIApplication.shared.keyWindow) }
func endImageRectForpresent(indexPath: IndexPath) -> CGRect { let imageUrl = URL(string: imageVM.imageArray[indexPath.item].pic74)!
var image = KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: imageUrl.absoluteString) if image == nil { image = UIImage(named: "coderJun") }
let imageH = kScreenWidth / image!.size.width * image!.size.height let y: CGFloat = imageH < kScreenHeight ? (kScreenHeight - imageH) / 2 : 0
return CGRect(x: 0, y: y, width: kScreenWidth, height: imageH) } }
|
在图片展示界面
- 遵循并实现相关dismiss协议方法
- 该协议主要实现viewController返回到该图片对应的
IndexPath
所在的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| extension JunTranstionPhotoController: JunBrowserDismissDelegate{ func imageViewForDismiss() -> UIImageView { let imageV = UIImageView() imageV.contentMode = .scaleAspectFill imageV.clipsToBounds = true
imageV.image = baseImage.image imageV.frame = baseImage.convert(baseImage.frame, to: UIApplication.shared.keyWindow)
return imageV }
func indexPathForDismiss() -> IndexPath { return IndexPath(item: currentIndex, section: 0) } }
|
- 注意:
- 这里只是列出了主要的核心代码,具体的代码逻辑请参考demo
- 文中相关介绍有的地方如果有不是很详细或者有更好建议的,欢迎联系小编