CoreAnimation 细说动画(一)

总结下CoreAnimation的动画使用方法:

1.最简单的实现方法,

    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;  //将transform属性重置为标示本质上“没有变化”的值
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    animationView.frame = CGRectMake(15, 330, 62, 90);
    [UIView commitAnimations];

实现了正方形从上到下的移动过程.

2.重复动画

    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:1];
    [UIView setAnimationRepeatAutoreverses:YES];  //自动反向
    [UIView setAnimationRepeatCount:2];  //重复次数
    animationView.frame = CGRectMake(15, 330, 62, 90);
    [UIView commitAnimations];

首先实现了上一个例子的动画效果,从上到下的移动效果,然后紧接着以同样的持续时间反方向播放一遍,接下来重复一次。这个动画过程持续4秒。重复次数可以为小数,也就是说动画过程不一定完整。

3.设置委托,动画结束调用方法

 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop)];
    animationView.frame = CGRectMake(15, 330, 62, 90);
    [UIView commitAnimations];
 
	// Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop
{
    NSLog(@"The Animation Stop");
}

设置了代理,设置了动画结束时调用的方法,动画结束后会调用次方法,以便执行其它方法。此时调用的方法为无参数方法,下面看下有参数方法的使用方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    animationView.frame = CGRectMake(15, 330, 62, 90);
    [UIView commitAnimations];
 
	// Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop:(NSString *)animation finish:(BOOL)flag context:(void *)context
{
    NSLog(@"The Animation Stop");
}

这里时animationDidStop委托的标准签名方法,如果此方法涉及到参数,那么参数类型必须匹配该签名。否则可能导致崩溃。
涉及到三个参数:必须是一个指向NSString实例的指针,传入的就是设置的动画名称,即ViewAnimationOne。
第二个参数:如果有此参数,必须是一个Boolean标志,如果动画执行完成这个值就为YES,否则为NO。
第三个参数:必须是一个void指针,对于调用动画委托的当前动画,这个指针会被设置为传入的context。

PS:第一个参数我设置成了数组竟然也是可以的.

在看这个例子:

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:1];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    animationView.frame = CGRectMake(15, 330, 62, 90);
      animationView.alpha = 0;
    [UIView commitAnimations];
 
	// Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop:(NSString *)animation finish:(BOOL)flag context:(void *)context
{
    if(flag){
        NSLog(@"1");
    }else{
        NSLog(@"0");
    }
 
    ((UIView *) context).alpha = 1.0;
    NSLog(@"The Animation Stop---%@---%@",animation,context);
}

自己运行就会看到效果了。

延时

[UIView setAnimationDelay:5];  //延时5秒开始动画

UIView变换

这里通过transform的属性和Core Graphics函数,改变UIKit组件的大小(缩放),位置(平移)和旋转。

CGAffineTransformMakeScale 改变组件大小,此函数接受两个缩放值,分别用于改变组件的宽度和高度。参数值可以为小数。
CGAffineTransformMakeTranslation 相对原始位置改变组件的位置。传递的单位分别为x轴和y轴方向的相对位移,x.y可以为小数
CGAffineTransformMakeRotation 围绕组件的中心坐标进行旋转。传递的参数为要旋转的弧度数,可以为小数。

PS:弧度为公制单位,180度=3.14159弧度

角度转弧度公示:角度*PI/180 在IOS操作系统中,正值标示逆时针旋转,负值标示顺时针旋转;

下面来看下具体的例子:

//缩放
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
//    [UIView setAnimationDelay:5];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    animationView.transform = CGAffineTransformMakeScale(1.25, 1.25); //放大为原来的1.25倍,重复动画
    [UIView commitAnimations];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop:(NSString *)animation finish:(BOOL)flag context:(void *)context
{
    if(flag){
        NSLog(@"1");
    }else{
        NSLog(@"0");
    }
 
    ((UIView *) context).alpha = 1.0;
    NSLog(@"The Animation Stop---%@---%@",animation,context);
}
 
//----------------------------------------------
 
//平移
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
//    [UIView setAnimationDelay:5];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    animationView.transform = CGAffineTransformMakeTranslation(100, 0);
    [UIView commitAnimations];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop:(NSString *)animation finish:(BOOL)flag context:(void *)context
{
    if(flag){
        NSLog(@"1");
    }else{
        NSLog(@"0");
    }
 
    ((UIView *) context).alpha = 1.0;
    NSLog(@"The Animation Stop---%@---%@",animation,context);
}
 
//---------------------------------------------------------
 
//旋转
 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
//    [UIView setAnimationDelay:5];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    animationView.transform = CGAffineTransformMakeRotation(150*M_PI/180);
    [UIView commitAnimations];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop:(NSString *)animation finish:(BOOL)flag context:(void *)context
{
    if(flag){
        NSLog(@"1");
    }else{
        NSLog(@"0");
    }
 
    ((UIView *) context).alpha = 1.0;
    NSLog(@"The Animation Stop---%@---%@",animation,context);
}

上面的都是单一的动画,动画也可以进行组合,看下面的例子:

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
 
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = CGRectMake(15, 144, 62, 90);
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
//    [UIView setAnimationDelay:5];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    CGAffineTransform transformone = CGAffineTransformMakeScale(2, 2);
    CGAffineTransform transformtwo = CGAffineTransformMakeRotation(150*M_PI/180);
    animationView.transform = CGAffineTransformConcat(transformone, transformtwo);
    [UIView commitAnimations];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimationStop:(NSString *)animation finish:(BOOL)flag context:(void *)context
{
    if(flag){
        NSLog(@"1");
    }else{
        NSLog(@"0");
    }
 
    ((UIView *) context).alpha = 1.0;
    NSLog(@"The Animation Stop---%@---%@",animation,context);
}

   转载自:舵手网络--http://www.helmsmansoft.com

   本文链接地址: http://www.helmsmansoft.com/index.php/archives/1491


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值