iOS animations: UIView animations and Calayer animations (use of cabasicanimation, cakeyframeanimation)

Source: Internet
Author: User

There are two ways to implement animations in iOS, one is uiview to animate, the other is implemented by Calayer, and the following is a simple implementation of two animations:

First, the realization of UIView animation

UIView using context for animation

Key code:

123456789101112131415161718192021222324252627282930 //参数1 动画名称 参数2 要实现动画的对象上下文        [UIView beginAnimations:@"attribute"context:_showImageView];        //设置动画的时间    [UIView setAnimationDuration:1.0f];        //设置动画延迟时间//    [UIView setAnimationDelay:2];        //设置视图center 实现试图移动动画    _showImageView.center = CGPointMake(100, 100);        //设置alpha值:视图透明度    _showImageView.alpha = 0.2f;        //设置背景颜色    _showImageView.backgroundColor = [UIColor greenColor];        //UIView动画 设置代理    [UIView setAnimationDelegate:self];        //动画将要开始代理方法    [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];        //动画已经结束代理方法    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];        //提交动画设置,执行动画    [UIView commitAnimations];

Animations that are implemented using block:

1234567891011121314 //UIView动画, 使用Block实现    [UIView animateWithDuration:1.0f animations:^{                //通过设置translation 实现视图的偏移        if([self.mySwitch isOn]) {                        //基于上一次的translation            _showImageView.transform = CGAffineTransformTranslate(_showImageView.transform, 50, 0);        else{                        //基于原始的translation            _showImageView.transform = CGAffineTransformMakeTranslation(-50, 0);        }    }];

Second, the realization of Calayer animation

Cabasic animations: Determining animations based on initial position and end position

123456 //CABasic 有两个属性 fromValue 动画开始值,toValue动画结束值    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"position"];    [animation1 setDuration:2];    animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 150)];    animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];    [_imageView.layer addAnimation:animation1 forKey:@"position"];

Create a set of animations:

12345678910111213141516171819 //创建组动画对象    CAAnimationGroup *group = [CAAnimationGroup animation];         //CABasic动画    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];    animation1.fromValue = @1.5;    animation1.toValue = @0.5;        //关键帧动画    CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];    animation2.values = @[[NSValue valueWithCGPoint:CGPointMake(100, 100)],                         [NSValue valueWithCGPoint:CGPointMake(200, 150)],                         [NSValue valueWithCGPoint:CGPointMake(100, 200)],                         [NSValue valueWithCGPoint:CGPointMake(200, 250)]];        //group添加动画数组,group中动画对象并发执行    [group setAnimations:@[animation1, animation2]];    [group setDuration:4.0f];    [_imageView.layer addAnimation:group forKey:@"group"];

iOS animations: UIView animations and Calayer animations (use of cabasicanimation, cakeyframeanimation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.