当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS实现类似格瓦拉电影的转场动画

iOS实现类似格瓦拉电影的转场动画

2019年07月24日  | 移动技术网IT编程  | 我要评论

蜱虫狗图片,lass.exe,上海展会设计

用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下


自定义转场动画

首先就要声明一个遵守uiviewcontrolleranimatedtransitioning协议的类.

然后实现协议中的两个函数

// this is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// 返回转场时间
- (nstimeinterval)transitionduration:(nullable id <uiviewcontrollercontexttransitioning>)transitioncontext;
// 转场的动作
- (void)animatetransition:(id <uiviewcontrollercontexttransitioning>)transitioncontext;

push和pop都在animatetransition:里面实现,所以需要一个参数表示是push还是pop;还有一个参数表示转场时间

#import <uikit/uikit.h>
#import <foundation/foundation.h>


typedef enum : nsuinteger {
 animate_push = 0,
 animate_pop = 1,

} animate_type;

@interface sftrainsitionanimate : nsobject<uiviewcontrolleranimatedtransitioning>

- (instancetype)initwithanimatetype:(animate_type)type andduration:(cgfloat)dura;

@property (assign, nonatomic) cgfloat duration;
@property (assign, nonatomic) animate_type type;

@end

那么要如何使用新建的对象呢?可以通过uinavigationcontrollerdelegate中的

- (id<uiviewcontrolleranimatedtransitioning>)navigationcontroller:(uinavigationcontroller *)navigationcontroller animationcontrollerforoperation:(uinavigationcontrolleroperation)operation fromviewcontroller:(uiviewcontroller *)fromvc toviewcontroller:(uiviewcontroller *)tovc{
 if (operation == uinavigationcontrolleroperationpush) {
 self.animate = [[sftrainsitionanimate alloc] init];
 return self.animate;
 }else{
 return nil;
 }

}

当然,要调用这个方法还得先把uinavigationcontrollerdelegate委托给视图控制器

self.navigationcontroller.delegate = self;

再来看看uiviewcontrolleranimatedtransitioning这个协议,返回时间的方法就不介绍了。重点在

- (void)animatetransition:(id<uiviewcontrollercontexttransitioning>)transitioncontext{
 //起始视图控制器
 uiviewcontroller *fromvc = [transitioncontext viewcontrollerforkey:uitransitioncontextfromviewcontrollerkey];
 //目标视图控制器
 uiviewcontroller *tovc = [transitioncontext viewcontrollerforkey:uitransitioncontexttoviewcontrollerkey];
 //在这个视图上实现跳转动画
 uiview *containview = [transitioncontext containerview];
}

如上注释,我们可以通过参数transitioncontext获取到起始和目标控制。并在containview这个视图实现我们想要实现的动画。

接下来就是转场动画的实现了,push动画的流程大概就是:点击第一个页面的一个控件,模拟出控件然后移动到第二个界面同一个样式控件的位置,之后再把第二个界面以控件的位置中心扩散显示出来。

那么现在我们就在协议方法中通过fromvc获取到第一个视图的控件,并制造一个镜像视图移动到tovc的目标控件的位置。在这里,我调用了uiviewcontroller分类关联一个对象,用来记录控件(非拥有关系)。

#import <uikit/uikit.h>
#import <foundation/foundation.h>

@interface uiviewcontroller (sftrainsitionextension)

@property (assign, nonatomic) cgfloat sf_targetheight;//灰白背景的分割线高度
@property (weak , nonatomic) uiview *sf_targetview;

@end

产生targetview镜像:

//产生targetview镜像
- (uiview *)customsnapshofromview:(uiview *)inputview {

 // make an image from the input view.
 uigraphicsbeginimagecontextwithoptions(inputview.bounds.size, no, 0);
 [inputview.layer renderincontext:uigraphicsgetcurrentcontext()];
 uiimage *image = uigraphicsgetimagefromcurrentimagecontext();
 uigraphicsendimagecontext();

 // create an image view.
 uiview *snapshot = [[uiimageview alloc] initwithimage:image];
 snapshot.layer.maskstobounds = no;
 snapshot.layer.cornerradius = 0.0;
 snapshot.layer.shadowoffset = cgsizemake(0.0, 0.0);
 snapshot.layer.shadowradius = 5.0;
 snapshot.layer.shadowopacity = 0.4;

 return snapshot;
}

现在就可以制作移动的动画:

//起始位置
 cgrect originframe = [fromvc.sf_targetview convertrect:fromvc.sf_targetview.bounds toview:fromvc.view];
 //动画移动的视图镜像
 uiview *customview = [self customsnapshofromview:fromvc.sf_targetview];
 customview.frame = originframe;

 //移动的目标位置
 cgrect finishframe = [tovc.sf_targetview convertrect:tovc.sf_targetview.bounds toview:tovc.view];

 uiview *containview = [transitioncontext containerview];

 //背景视图 灰色高度
 cgfloat height = cgrectgetmidy(finishframe);
 tovc.sf_targetheight = height;

 //背景视图 灰色
 uiview *backgray = [[uiview alloc] initwithframe:cgrectmake(0, 0, k_sf_screen_width, k_sf_screen_hight)];
 backgray.backgroundcolor = [uicolor lightgraycolor];
 //背景视图 白色
 uiview *backwhite = [[uiview alloc] initwithframe:cgrectmake(0, height, k_sf_screen_hight, k_sf_screen_hight-height)];
 backwhite.backgroundcolor = [uicolor whitecolor];

 tovc.view.frame = [transitioncontext finalframeforviewcontroller:tovc];

 //注意添加顺序
 [containview addsubview:tovc.view];
 [containview addsubview:backgray];
 [backgray addsubview:backwhite];
 [containview addsubview:customview];

 //动画
 [uiview animatewithduration:_duration/3 animations:^{
 customview.frame = finishframe;
 customview.transform = cgaffinetransformmakescale(1.1, 1.1);
 } completion:^(bool finished) {
 if (finished) {

  [uiview animatewithduration:_duration/3 animations:^{

  customview.transform = cgaffinetransformidentity;

  } completion:^(bool finished) {
  if (finished) {
   [uiview animatewithduration:_duration/3 animations:^{
   customview.alpha = 0.0;
   } completion:^(bool finished) {
   if (finished) {
    [backgray removefromsuperview];
    [customview removefromsuperview];
    [transitioncontext completetransition:yes];
   }
   }];

   [self addpathanimatewithview:backgray frompoint:customview.center];

  }
  }];

移动完之后,要以圆形扩散显示出push之后的界面。就可以通过uibezierpathcashapelayer来实现。uibezierpath表示路径,cashapelayer可以根据路径来显示区域,那么我们可以以第一个界面的视图先看看效果。

 uibezierpath *path = [uibezierpath bezierpathwithrect:self.collectionview.bounds];

 [path appendpath:[uibezierpath bezierpathwitharccenter:self.collectionview.center radius:50 startangle:0 endangle:2*m_pi clockwise:no]];

 cashapelayer *layer = [cashapelayer layer];
 layer.path = path.cgpath;
 self.collectionview.layer.mask = layer;

初始化path路径以collectionview的四边画一个路径,然后加入一个以collectionview的中心为圆点,半径为50的路径,显示的区域就为两个路径之间的区域。

然后再把代码中的radius大小设为200


现在,我们创建一个cabasicanimation对象去完成扩散的动画,起始位置是半径为10的圆,终点位置是半径为200的圆.

 uibezierpath *path = [uibezierpath bezierpathwithrect:self.collectionview.bounds];

 [path appendpath:[uibezierpath bezierpathwitharccenter:self.collectionview.center radius:10 startangle:0 endangle:2*m_pi clockwise:no]];

 uibezierpath *path2 = [uibezierpath bezierpathwithrect:self.collectionview.bounds];

 [path2 appendpath:[uibezierpath bezierpathwitharccenter:self.collectionview.center radius:200 startangle:0 endangle:2*m_pi clockwise:no]];

 cashapelayer *layer = [cashapelayer layer];
 self.collectionview.layer.mask = layer;

 cabasicanimation *pathanimation = [cabasicanimation animationwithkeypath:@"path"];
 pathanimation.fromvalue = (__bridge id)path.cgpath;
 pathanimation.tovalue = (__bridge id)path2.cgpath;
 pathanimation.duration = 1.0;
 pathanimation.repeatcount = 1;
 pathanimation.removedoncompletion = no;
 pathanimation.fillmode = kcafillmodeforwards;
 pathanimation.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseout];
 [layer addanimation:pathanimation forkey:@"pathanimate"];


现在就可以完成push的 转场效果了。注意圆圈白色部分显示的是collectionview底部的self.view的视图。

//加入收合动画
- (void)addpathanimatewithview:(uiview *)toview frompoint:(cgpoint)point{
 //create path
 uibezierpath *path = [uibezierpath bezierpathwithrect:cgrectmake(0, 0, k_sf_screen_width, k_sf_screen_hight)];
 //create path
 [path appendpath:[uibezierpath bezierpathwitharccenter:point radius:0.1 startangle:0 endangle:2*m_pi clockwise:no]];

 cgfloat radius = point.y > 0?k_sf_screen_hight*3/4: k_sf_screen_hight*3/4-point.y;
 uibezierpath *path2 = [uibezierpath bezierpathwithrect:cgrectmake(0, 0, k_sf_screen_width, k_sf_screen_hight)];
 [path2 appendpath:[uibezierpath bezierpathwitharccenter:point radius:radius startangle:0 endangle:2*m_pi clockwise:no]];

 cashapelayer *shapelayer = [cashapelayer layer];
 //shapelayer.path = path.cgpath;
 toview.layer.mask = shapelayer;

 cabasicanimation *pathanimation = [cabasicanimation animationwithkeypath:@"path"];
 pathanimation.fromvalue = _type == animate_push? (__bridge id)path.cgpath:(__bridge id)path2.cgpath;
 pathanimation.tovalue = _type == animate_push? (__bridge id)path2.cgpath:(__bridge id)path.cgpath;
 pathanimation.duration = _duration/3;
 pathanimation.repeatcount = 1;
 pathanimation.removedoncompletion = no;
 pathanimation.fillmode = kcafillmodeforwards;
 pathanimation.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseout];
 [shapelayer addanimation:pathanimation forkey:@"pathanimate"];
}

pop动画其实和push差不多,这里就不说了。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网