iOS之View动画

转载自:http://www.cnblogs.com/YouXianMing/p/4222587.html

对于view延时执行了两个动画后,会将第一个动画效果终止了,直接在第一个动画的view的最后的状态上接执行后续的动画效果,也就是说,我们可以利用这个特性来写分段动画效果,比如,可以定时2秒,2秒的状态值为100%,中途可以停止,达不到2秒的效果就触发不了最终效果,这对于写控件来说是很好的一个属性哦,下次教程将会写一个按钮的特效的控件,效果类似于:

效果:

源码:

复制代码
//
//  ViewController.m
//  ViewAnimation
//
//  Created by YouXianMing on 15/1/13.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView   *showView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    // 添加view
    self.showView                 = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 0, 30)];
    self.showView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.showView];
    
    // 1s后执行变长动画
    [self performSelector:@selector(viewAnimationOne) withObject:nil afterDelay:1];
    
    // 2s后执行缩小动画
    [self performSelector:@selector(viewAnimationTwo) withObject:nil afterDelay:2];
}

- (void)viewAnimationOne {

    // 动画时间长度为3s
    [UIView animateWithDuration:3.f animations:^{
        self.showView.frame = CGRectMake(10, 100, 0 + 300, 30);
    } completion:^(BOOL finished) {
        NSLog(@"动画1结束 %@", NSStringFromCGRect(self.showView.frame));
    }];
    
    
}

- (void)viewAnimationTwo {
    
    // 动画时间长度为1s
    [UIView animateWithDuration:1.f animations:^{
        self.showView.frame = CGRectMake(10, 100, 0, 30);
    } completion:^(BOOL finished) {
        NSLog(@"动画2结束 %@", NSStringFromCGRect(self.showView.frame));
    }];
}


@end
复制代码

 

@interface ViewController ()
@property (nonatomic, strong) UIView   *showView;
@property (nonatomic, strong) UIButton    *button;
@property (nonatomic, strong) UILabel     *showLabel;
@property (nonatomic, strong) UILabel     *staticLabel;

@end

@implementation ViewController
{
    UIButton* _btn1;
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    
    self.showView                        = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 0)];
    self.showView.transform              = CGAffineTransformMakeRotation(45 * M_PI / 180.0);//选择角度
//    self.showView.transform              = CGAffineTransformMakeRotation(90 * M_PI / 180.0);
    self.showView.userInteractionEnabled = NO;
    self.showView.backgroundColor        = [UIColor redColor];
    
    
    self.staticLabel               = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
    self.staticLabel.text          = @"YouXianMing";
    self.staticLabel.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.f];
    self.staticLabel.textAlignment = NSTextAlignmentCenter;
    self.staticLabel.textColor     = [UIColor redColor];
    
    
    self.showLabel               = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
    self.showLabel.text          = @"YouXianMing";
    self.showLabel.alpha         = 0.f;
    self.showLabel.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.f];
    self.showLabel.textAlignment = NSTextAlignmentCenter;
    self.showLabel.textColor     = [UIColor blackColor];
    
    
    
    // 完整显示按住按钮后的动画效果
    _button                     = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
    _button.backgroundColor     = [UIColor clearColor];
    _button.layer.borderWidth   = 1.f;
    _button.layer.borderColor   = [UIColor redColor].CGColor;
    _button.layer.masksToBounds = YES;
    _button.center              = self.view.center;
    [self.view addSubview:_button];
    
    self.showView.center = CGPointMake(_button.frame.size.width / 2.f, _button.frame.size.height / 2.f);
    [_button addSubview:self.showView];
    
    self.staticLabel.center = CGPointMake(_button.frame.size.width / 2.f, _button.frame.size.height / 2.f);
    [_button addSubview:self.staticLabel];
    
    self.showLabel.center = CGPointMake(_button.frame.size.width / 2.f, _button.frame.size.height / 2.f);
    [_button addSubview:self.showLabel];
    
    
    // 按住按钮后没有松手的动画
    [_button addTarget:self
                action:@selector(scaleToSmall)
      forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
    
    // 按住按钮松手后的动画
    [_button addTarget:self
                action:@selector(scaleAnimation)
      forControlEvents:UIControlEventTouchUpInside];
    
    // 按住按钮后拖拽出去的动画
    [_button addTarget:self
                action:@selector(scaleToDefault)
      forControlEvents:UIControlEventTouchDragExit];

}

- (void)scaleToSmall {
    [UIView animateWithDuration:0.5f animations:^{
        self.showView.bounds          = CGRectMake(0, 0, 300, 90);
        self.showView.backgroundColor = [UIColor redColor];
        self.showLabel.alpha          = 1.f;
    } completion:^(BOOL finished) {
        NSLog(@"%d", finished);
    }];
}

- (void)scaleAnimation {
    // 获取到当前的状态
    self.showView.bounds                = ((CALayer *)self.showView.layer.presentationLayer).bounds;
    self.showView.layer.backgroundColor = ((CALayer *)self.showView.layer.presentationLayer).backgroundColor;
    self.showLabel.alpha                = ((CALayer *)self.showLabel.layer.presentationLayer).opacity;
    
    // 移除之前的动画状态
    [self.showView.layer removeAllAnimations];
    
    // 做全新的动画
    [UIView animateWithDuration:0.25f animations:^{
        self.showView.bounds          = CGRectMake(0, 0, 300, 0);
        self.showView.backgroundColor = [UIColor clearColor];
        self.showLabel.alpha          = 0.f;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)scaleToDefault {
    // 获取到当前的状态
    self.showView.bounds                = ((CALayer *)self.showView.layer.presentationLayer).bounds;
    self.showView.layer.backgroundColor = ((CALayer *)self.showView.layer.presentationLayer).backgroundColor;
    self.showLabel.alpha                = ((CALayer *)self.showLabel.layer.presentationLayer).opacity;
    
    // 移除之前的动画状态
    [self.showView.layer removeAllAnimations];
    
    // 做全新的动画
    [UIView animateWithDuration:0.25f animations:^{
        self.showView.bounds          = CGRectMake(0, 0, 300, 0);
        self.showView.backgroundColor = [UIColor clearColor];
        self.showLabel.alpha          = 0.f;
    } completion:^(BOOL finished) {
        
    }];
}

源码:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值