博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIViewAnimation
阅读量:7212 次
发布时间:2019-06-29

本文共 3657 字,大约阅读时间需要 12 分钟。

在ViewController.m中声明

@interface ViewController ()

@property (nonatomic, strong) UIView *myView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
 
    
    //UIView层的动画分为: 代码块(begin...commit) , block块(常用)
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myView];
    
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    //第一种(比较常用,)

// 这里要写上(防止循环引用) 这个也可以__weak ViewController *myself = self;

    __weak typeof(self) pSelf = self;

 //第一个参数animateWithDuration:代表的是动画的执行时间

    //block块里面代表的是执行的动画
   [UIView animateWithDuration:2.0f animations:^{
        UIViewAnimation
        //设置frame有动画 点击屏幕后,宽高会发生变化
        pSelf.myView.frame = CGRectMake(100, 100, 200, 200);
       
 
       
      
       //改变bounds的时候也可以执行动画,但是是以试图的center为中心像两边扩充
     //  pSelf.myView.bounds = CGRectMake(0, 0, 200, 200);
      
        //设置视图的透明度
       pSelf.myView.alpha = 0.1;
      
      //中心
//pSelf.myView.center = pSelf.view.center;
       
        //设置边框
 //pSelf.myView.layer.borderWidth = 10;
    }];
  

******************************************第二种(在第一种的基础之上添加了一个参数)*********************************************************

 //第一个参数:代表的时间
    //第二个参数:block代表执行的动画
    //第三个参数:block代表动画完成
    [UIView animateWithDuration:1.0f animations:^{
      
            pSelf.myView.backgroundColor = [UIColor greenColor];
   } completion:^(BOOL finished) {
      
     
       //这里的代码不会当作动画执行
       pSelf.myView.backgroundColor = [UIColor redColor];
    }];
  

 

//************************************第三种(添加一个延迟)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         **********************************************

    
    //第一个参数:代表的执行时间
    //第二个参数delay:代表的是延迟多久执行
   //第三个参数:代表的是动画的一些特效(此处填的是重复执行)

//点击鼠标后开始执行的东西

    [UIView animateWithDuration:2.0f delay:1.0f options:   UIViewAnimationOptionRepeat  animations:^{
       
        pSelf.myView.center = pSelf.view.center;
        pSelf.myView.backgroundColor = [UIColor greenColor];
       

       //鼠标屏幕后,接下来要执行的东西

       } completion:^(BOOL finished) {
       
       pSelf.myView.backgroundColor = [UIColor yellowColor];
      
    }];

 

//**********************************第四个******************************************************************

    //usingSpringWithDamping :0~1 ,值越小,越明显 ,代表的是阻尼系数,值越大,效果越小
    //initialSpringVelocity:代表的是初始速度

//点击鼠标后开始执行的东西

  [UIView animateWithDuration:2.0f delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:50 options:UIViewAnimationOptionRepeat animations:^{
      
        pSelf.myView.frame = CGRectMake(100, 300, 200, 100);
 
      
       //鼠标屏幕后,接下来要执行的东西
    } completion:^(BOOL finished) {
       
       
        pSelf.myView.backgroundColor = [UIColor yellowColor];
      
   }];
  

 

 //********代码块

    
   //第一个参数:代表的是动画的名字
    //第二个参数:代表的是上下文相关的,目前没用,以后不确定
    [UIView beginAnimations:nil context:nil];
    
    //从当前状态开始
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    //设置动画在开始和结束的时候的一些特效
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    //设置动画的持续时间
    [UIView setAnimationDuration:3.0f];
    
   //设置动画的延迟时间
    //[UIView setAnimationDelay:10.0f];
    
    
    //设置动画的重复次数
    [UIView setAnimationRepeatCount:1000];
    
    //设置动画的代理人
    [UIView setAnimationDelegate:self];
    
    //设置动画将要开始,会执行的SEL方法
    //[UIView setAnimationWillStartSelector:@selector(<#selector#>)];
    //设置动画将要结束,会执行的SEL方法
    //UIView setAnimationDidStopSelector:@selector(<#selector#>);
    self.myView.backgroundColor = [UIColor greenColor];
    
    self.myView.frame = CGRectMake(100, 100, 200, 200);
    
    //提交动画,才会执行动画
    [UIView commitAnimations];

 

 

  

    //*****************UIView切换的动画(不常用)
    //第一个参数:要变没的视图
    //第二个参数:要变出的视图
    //第三个参数:变出的动画动作
    
    [UIView transitionFromView:<#(UIView *)#> toView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> completion:<#^(BOOL finished)completion#>];

 

    //比较常用的

 //可以配合着切换tableview与collectionView

[UIView transitionWithView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>]

 
}

 

转载于:https://www.cnblogs.com/Coder-GT/p/4875869.html

你可能感兴趣的文章
ASP.NET MVC在IIS6下部署的小技巧
查看>>
asp.net 递归删除文件夹及其子文件夹和所有文件[转]
查看>>
TCP端口状态说明ESTABLISHED、TIME_WAIT、 CLOSE_WAIT
查看>>
Bengio:我留在学术圈为全人类作贡献,而不是为某一个公司赚钱
查看>>
100多个经典常用的PHP功能插件大全实例演示和下载
查看>>
Mac 下iterm2配色方案(高亮)及显示分支
查看>>
使用<meta>来刷新网页效果
查看>>
VR为难民发声,传递人道主义精神
查看>>
基准测试工具
查看>>
遇到的几个开机启动故障
查看>>
NEC向格鲁吉亚提供基于面部识别技术的城市监控系统
查看>>
linux网络编程之-----基础理论篇
查看>>
加大Linux服务器的文件描述符
查看>>
Linux内核编译过程详解
查看>>
shell监控web服务的多种方案
查看>>
微信小程序--亲戚称呼计算
查看>>
Broker模块划分
查看>>
常用URL地址
查看>>
修改centos系统默认编辑器
查看>>
Linux 2 unit7 挂载网络共享
查看>>