1.iOS中我们能看到的控件都是UIView的子类,比如UIButton UILabel UITextField UIImageView等等
2.UIView能够在屏幕的显示是因为在创建它的时候内部自动添加一个CALayer图层,通过这个图层在屏幕上显示的时候会调用一个drawRect: 的方法,完成绘图,才能在屏幕上显示
3.CALayer 本身就具有显示功能,但是它不能响应用户的交互事件,如果只是单纯的显示一个图形,此时你可以使用CALayer创建或者是使用UIView创建,但是如果这个图形想响应用户交互事件,必须使用UIView或者子类
动画知识框图如下:
- #import "ViewController.h"
- #import "UITextField+Shake.h"
- @interface ViewController ()
- @property (retain, nonatomic) IBOutlet UIImageView *balloon;
- @property (retain, nonatomic) IBOutlet UITextField *TF;
- @property (retain, nonatomic) IBOutlet UIButton *bounces;
- @property (retain, nonatomic) IBOutlet UIView *animationView;
- @property (retain, nonatomic) IBOutlet UIImageView *cloud;
- @end<span style="font-family: 'STHeiti Light';">@implementation ViewController</span>
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- CALayer *layer = self.view.layer;
-
-
-
- self.animationView.layer.backgroundColor = [UIColor greenColor].CGColor;
- }
//给TF创建一个类目:
- UITextField+Shake.h
- #import <UIKit/UIKit.h>
- @interface UITextField (Shake)
- - (void)shake;
- @end
-
- UITextField+Shake.m
- #import "UITextField+Shake.h"
-
- @implementation UITextField (Shake)
-
- - (void)shake{
- CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
- keyFrame.values = @[@(self.center.x + 10),@(self.center.x),@(self.center.x - 10)];
- keyFrame.repeatCount = 10;
- keyFrame.duration = 0.03;
- [self.layer addAnimation:keyFrame forKey:nil];
-
- }
- @end
开始动画按钮点击事件
- - (IBAction)handleAnimation:(UIButton *)sender {
-
- [self handlePropertyAnimation];
-
-
- [self handlePrepertyAnimationBlock];
-
-
- [self handleTrabsitionAnimation];
-
-
- [self handleCALayer];
-
-
- [self handleBasicAnimation];
-
- [self handleKeyFrameAnimation];
-
- [self.TF shake];
-
-
- [self handleLayerCATransactionAnimation];
-
- [self handleAnimatonGroup];
-
- }
//UIView的属性动画 可动画的属性 : frame center bounds alpha backgroundColor transfrom
//修改属性做动画,动画结束后属性修改的结果是真实的作用到动画的视图上,不能恢复到之前的样子
- - (void)handlePropertyAnimation{
-
-
- [UIView beginAnimations:nil context:nil];
-
- [UIView setAnimationDelegate:self];
-
- [UIView setAnimationDuration:3.0];
-
- [UIView setAnimationRepeatCount:3.0];
-
- [UIView setAnimationRepeatAutoreverses:YES];
-
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
-
-
- [UIView setAnimationDidStopSelector:@selector(makeAnimationBack)];
-
-
-
- CGPoint center = self.animationView.center;
- center.y += 10;
- self.animationView.center =center;
-
- self.animationView.alpha = 0.0;
-
-
-
- self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, M_PI_4);
-
- self.animationView.transform = CGAffineTransformScale(self.animationView.transform, 0.5, 0.5);
-
-
- [UIView commitAnimations];
- }
//恢复到视图之前的状态
- - (void)makeAnimationBack{
-
-
- self.animationView.center = self.view.center;
-
- self.animationView.alpha = 1.0;
-
- self.animationView.transform = CGAffineTransformIdentity;
-
- }
-
-
- - (void)handlePrepertyAnimationBlock{
-
-
-
- __block typeof(self)weakSelf = self;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [UIView animateWithDuration:2 animations:^{
-
- CGPoint center = weakSelf.animationView.center;
-
- center.y += 50;
- weakSelf.animationView.center =center;
-
- weakSelf.animationView.alpha = 0.0;
-
- weakSelf.animationView.transform = CGAffineTransformScale(weakSelf.animationView.transform, 0.5, 0.2);
-
-
- } completion:^(BOOL finished) {
-
- [weakSelf makeAnimationBack];
- }];
-
-
-
-
-
-
-
- [UIView animateWithDuration:3 delay:1 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
-
- CGPoint center = weakSelf.animationView.center;
-
- center.y += 50;
- weakSelf.animationView.center =center;
-
- weakSelf.animationView.alpha = 0.0;
-
- weakSelf.animationView.transform = CGAffineTransformScale(weakSelf.animationView.transform, 0.5, 0.2);
-
- } completion:^(BOOL finished) {
-
- [weakSelf makeAnimationBack];
- }];
-
-
-
-
-
-
-
-
-
- [UIView animateWithDuration:2 delay:1 usingSpringWithDamping:0.5 initialSpringVelocity:500 options:UIViewAnimationOptionCurveEaseInOut animations:^{
- CGPoint center = weakSelf.bounces.center;
- center.y += 10;
- weakSelf.bounces.center = center;
-
- weakSelf.bounces.transform = CGAffineTransformScale(weakSelf.bounces.transform, 1.2, 1.2);
- } completion:^(BOOL finished) {
- CGPoint center = weakSelf.bounces.center;
- center.y -= 10;
- weakSelf.bounces.center = center;
- weakSelf.bounces.transform = CGAffineTransformIdentity;
-
- }];
- }
//UIView的过渡动画
- - (void)handleTrabsitionAnimation{
-
-
- __block typeof(self)weakSelf = self;
-
-
-
- [UIView transitionWithView:self.animationView duration:2 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
-
- weakSelf.animationView.transform = CGAffineTransformRotate(weakSelf.animationView.transform, M_PI_4);
-
- } completion:nil];
-
- }
//CALayer动画,修改layer层的属性做动画并没有真实的作用到这个视图上,动画知识一种假象
- - (void)handleCALayer{
-
-
- self.animationView.layer.borderWidth = 10.0;
-
- self.animationView.layer.borderColor = [UIColor redColor].CGColor;
-
-
-
-
-
-
-
- self.animationView.layer.contents = (id)[UIImage imageNamed:@"WDGJ785Q{`CKL4J}1{_4{(Y.jpg"].CGImage;
-
-
-
-
- self.animationView.layer.anchorPoint = CGPointMake(0.5, 0);
- self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, M_PI_4);
-
-
- self.animationView.layer.position = CGPointMake(160, 184);
- }
//CALayer 的动画基类:CAAnimation
//CABasicAnimation 基础动画
- - (void)handleBasicAnimation{
-
- CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.x"];
-
- basic.fromValue = @(-80);
- basic.toValue = @(400);
-
- basic.duration = 5.0;
-
- basic.repeatCount = 1000;
- [self.cloud.layer addAnimation:basic forKey:nil];
- }
//CAKeyFrameAnimation 关键帧动画
- - (void)handleKeyFrameAnimation{
- CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- CGPoint point1 = self.cloud.center;
- CGPoint point2 = CGPointMake(160, 100);
- CGPoint point3 = CGPointMake(270, self.cloud.center.y);
-
-
-
- NSValue *value1 = [NSValue valueWithCGPoint:point1];
- NSValue *value2 = [NSValue valueWithCGPoint:point2];
- NSValue *value3 = [NSValue valueWithCGPoint:point3];
- keyFrame.repeatCount = 1000;
- keyFrame.duration = 15.0;
- keyFrame.values = @[value1,value2,value3,value1];
- [self.cloud.layer addAnimation:keyFrame forKey:nil];
- }
//CATransition CALayer 的过度动画
- - (void)handleLayerCATransactionAnimation{
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CATransition *transition = [CATransition animation];
-
- transition.type = @"cameraIrisHollowClose";
-
- [self.view.layer addAnimation:transition forKey:nil];
-
- }
//CAAinmationGroup 分组动画
- - (void)handleAnimatonGroup{
-
- CAKeyframeAnimation *keyframePath = [CAKeyframeAnimation animationWithKeyPath:@"position"];
-
-
- CGFloat radius = [UIScreen mainScreen].bounds.size.height / 2.0;
-
-
-
-
-
- UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, radius) radius:radius startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
-
- keyframePath.path = path.CGPath;
-
-
- CAKeyframeAnimation *keyFrameScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
-
- keyFrameScale.values = @[@1.0,@1.2,@1.4,@1.6,@1.8,@1.6,@1.4,@1.2,@1.0];
-
- CAAnimationGroup *group = [CAAnimationGroup animation];
-
- group.animations = @[keyframePath,keyFrameScale];
-
- group.duration = 8;
- group.repeatCount = 1000;
-
-
- [self.balloon.layer addAnimation:group forKey:nil];
-
- }
最终效果: