ios 实现类似微信预览的转场效果

github地址:ZoomingTransition
相信大家都用过微信,在聊天时点击图片,会从图片弹出预览页。在预览页点击关闭,会缩回到原图片。今天带一起来实现这个效果。

1.先引入这个库
pod 'ZoomingTransition'
2.创建你自己的PushVC继承自ZoomPushVC,并实现ZoomTransitionAnimatorDelegate
class MyPushVC : ZoomPushVC,ZoomTransitionAnimatorDelegate{

var lastSelectedIndexPath: IndexPath? = nil

//your own code...


func transitionWillStart() {
    guard let lastSelected = self.lastSelectedIndexPath else { return }
    self.collectionView.cellForItem(at: lastSelected)?.isHidden = true
}

func transitionDidEnd() {
    guard let lastSelected = self.lastSelectedIndexPath else { return }
    self.collectionView.cellForItem(at: lastSelected)?.isHidden = false
}

func referenceImage() -> UIImage? {
    guard
        let lastSelected = self.lastSelectedIndexPath,
        let cell = self.collectionView.cellForItem(at: lastSelected) as? CustomCell
    else {
        return nil
    }

    return cell.imageView.image
}

func imageFrame() -> CGRect? {
    guard
        let lastSelected = self.lastSelectedIndexPath,
        let cell = self.collectionView.cellForItem(at: lastSelected) as? CustomCell
    
    else {
        return nil
    }
    
    return FrameHelper.getTargerFrame(originalView: cell.imageView, targetView: self.view)



}
     
}
3.当在UICollectionview点击图片时,记录下index
class MyPushVC : ZoomPushVC,ZoomTransitionAnimatorDelegate{

//with the code above ...

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    self.lastSelectedIndexPath = indexPath
    let vc = PreviewVC()
    vc.image = self.slices[indexPath.row]
    self.navigationController?.pushViewController(vc, animated: true)
    
}

}
4.创建你自己的PopVC,实现ZoomTransitionAnimatorDelegate
class PreviewVC: UIViewController,ZoomTransitionAnimatorDelegate{

//...your own code

//when back button clicked
@objc func close(){
    
    self.navigationController?.popViewController(animated: true)
   
}

func transitionWillStart() {
   // self.imageView.isHidden = true
    self.view.alpha = 0
}

func transitionDidEnd() {
  //  self.imageView.isHidden = true
    self.view.alpha = 1
}

func referenceImage() -> UIImage? {
    return FrameHelper.getScreenshot(with: self.view)
}

func imageFrame() -> CGRect? {
   
    return self.view.frame
}
}

这样就可以实现了,省去了自己写转场动画的烦恼,是不是很简单呢?
github地址:ZoomingTransition 欢迎点赞

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值