首页 > 代码库 > 显示动画,隐式动画、关键帧动画

显示动画,隐式动画、关键帧动画

概要

      一些简单的动画代理学习例子,包括显示、隐式、关键帧、关键帧路径四类动画。(感觉这个动画太复杂,学习简单的例子没继续了)


结果展示

技术分享

流程概要

    见代码


主要代码

//
//  ViewController.m
//  Animation
//
//  Created by arbboter on 14/12/20.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) UIImageView* head;
@property (nonatomic, retain) UIButton* implicitAnimation;
@property (nonatomic, retain) UIButton* explicitAnimation;
@property (nonatomic, retain) UIButton* keyFrameAnimation;
@property (nonatomic, retain) UIButton* resetHeatState;
@end

@implementation ViewController


- (void)resetHead
{
    [_head removeFromSuperview];
    [_head release];
    _head = nil;
    
    _head = [[UIImageView alloc] init];
    _head.image = [UIImage imageNamed:@"head.png"];
    _head.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:_head];
    
    /** 设置初始值小且半透明 */
    _head.frame = CGRectMake(10, 20, _head.image.size.width, _head.image.size.height);
    _head.transform = CGAffineTransformMakeScale(0.1, 0.1);
    _head.center = CGPointMake(self.view.center.x, _head.frame.size.height/2+20);
    _head.alpha = 0.03;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self resetHead];
    
    UIButton* btn = nil;
    /** 重置按钮 */
    btn = [[UIButton alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height-40, 80, 30)];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"重置动画" forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    _resetHeatState = btn;
    
    /** 隐式动画按钮 */
    btn = [[UIButton alloc] initWithFrame:CGRectMake(_resetHeatState.frame.origin.x + _resetHeatState.frame.size.width + 10, self.view.frame.size.height-40, 80, 30)];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"隐式动画" forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    _implicitAnimation = btn;
    
    /** 显示动画按钮 */
    btn = [[UIButton alloc] initWithFrame:CGRectMake(_implicitAnimation.frame.origin.x + _implicitAnimation.frame.size.width + 10, self.view.frame.size.height-40, 80, 30)];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"显示动画" forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    _explicitAnimation = btn;
    
    /** 关键帧动画按钮 */
    btn = [[UIButton alloc] initWithFrame:CGRectMake(_explicitAnimation.frame.origin.x + _explicitAnimation.frame.size.width + 10, self.view.frame.size.height-40, 80, 30)];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"关键帧" forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    _keyFrameAnimation = btn;
}

-(void)onAnimate:(id)sender
{
    CGPoint headCenter = _head.center;
    CGPoint viewCenter = self.view.center;
    if (sender == _implicitAnimation)
    {
        /** 隐式动画 */
        [UIView beginAnimations:nil context:NULL];
        _head.layer.affineTransform = CGAffineTransformMakeTranslation(0, viewCenter.y*3/2-headCenter.y);
        _head.layer.affineTransform = CGAffineTransformScale(_head.layer.affineTransform, 1, 1);
        _head.layer.opacity = 1;
        [UIView commitAnimations];
    }
    else if(sender == _explicitAnimation)
    {
        /** 显示动画 */
        
        /** 添加第一个动画【由透明渐变为清晰】 使用内置动画效果:opacity */
        CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opAnim.duration = 5.0;
        opAnim.fromValue = http://www.mamicode.com/[NSNumber numberWithFloat:0.1];>

项目工程

显示动画,隐式动画、关键帧动画