Core Animation学习

本文介绍了如何在iOS项目中使用CoreAnimation进行动画制作。通过UIKit和CoreAnimation两种方式创建动画效果,包括图片移动、旋转等,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Core animation 或者叫Quartz Core是一个Objective C类库,位于iOS框架的Media层。

 

在project中使用Core Animation时,首先要将QuartzCore.framework链接到project中,然后在使用animation的类中引入对应的h文件

#import <QuartzCore/QuartzCore.h>

 

添加动画:

1.使用UIKit中提供的animation API

UIKit中有一些接口可以创建简单的动画,例如:

 

    CGRect smallRect = CGRectMake(0, 0, 20, 30);
    CGRect bigRect = CGRectMake(0, 0, 200, 300);
    
    UIImageView *imgview = [[UIImageView alloc] initWithFrame:smallRect];
    imgview.backgroundColor = [UIColor redColor];
    [self.view addSubview:imgview];
    
    [UIView animateWithDuration:2 animations:^{
        imgview.frame = bigRect;
    }];
    
    [UIView animateWithDuration:2 animations:^{
        imgview.frame = bigRect;
    } completion:^(BOOL finished){
        if(finished){
            imgview.backgroundColor = [UIColor blueColor];
        }
    }];
    
    // set delay time,duration time,animation clock,completion block
    [UIView animateWithDuration:2 delay:5 options:0 animations:^{
        imgview.frame = bigRect;
    }completion:^(BOOL finished){
        if(finished){
            imgview.backgroundColor = [UIColor blueColor];
        }
    }];
 

 

 

2.使用Core Animation

使用Core Animation可以创建嵌套的对象,并且可以沿着x、y、z轴对它们旋转rotation、缩放scale和转换transform。使用Core animation,你可以创建动态的用户界面而不用使用更底层的图形API,如OpenGL ES。

相关参考:

Core Animation类体系

 

Core Animation核心动画以及基本原理

 

隐式动画和显式动画

 

//============================================

一张图片在一定范围内,水平左右移动:

	[self.imageView setImage:[UIImage imageNamed:@"推荐页登录按钮.png"]];
        CABasicAnimation *theAnimation;    
        theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        theAnimation.duration=1;
        theAnimation.repeatCount=INT_MAX;
        theAnimation.autoreverses=YES;
        theAnimation.fromValue=[NSNumber numberWithFloat:0];
        theAnimation.toValue=[NSNumber numberWithFloat:-60];
        [self.imageView.layer addAnimation:theAnimation forKey:@"animateLayer"]; 

一张图片逆时针旋转

       [self.imageView setImage:[UIImage imageNamed:@"推荐页登录按钮.png"]];
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
        animation.delegate = self;
        animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI , 0, 0, 1.0)];
        animation.duration = 1;
        animation.cumulative = YES;
        animation.repeatCount = INT_MAX;
        [self.imageView.layer addAnimation:animation forKey:@"animation"];

  

Core Animation相关动画

Core Animation教学:关键桢动画

Core Animation教学:窗口淡入淡出特效

Core Animation教学:使用Transitions制作带动画效果的向导对话框

 

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [_headerView.activityIndicator stopAnimating];
    _headerView.hidden = YES;
    
    CGRect tFrame = self.tableView.frame;
    CGRect hFrame = _headerView.frame;
    self.tableView.frame = CGRectMake(hFrame.origin.x, hFrame.origin.y, tFrame.size.width, tFrame.size.height);
    
    [UIView commitAnimations];

 

 

 

 

   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值