iOS 过渡动画的实现



//
//  ViewController.m
//  test_CAGradientLayer
//
//  Created by admin on 3/4/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//

#import "ViewController.h"
#import "GardView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    GardView *view = [[GardView alloc] initWithFrame:CGRectMake(100, 300, 500, 50)];
//    view.frame = CGRectMake(100, 30, 50, 50);
    
    [self.view addSubview:view];
    
    [view setProgress:0.6];
    
    [view performAnimation];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


//
//  GardView.h
//  test_CAGradientLayer
//
//  Created by admin on 3/4/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface GardView : UIView

- (void)setProgress:(CGFloat)value;

- (void)performAnimation;

@end
//
//  GardView.m
//  test_CAGradientLayer
//
//  Created by admin on 3/4/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//

#import "GardView.h"

@interface GardView()

@property (nonatomic, strong) CALayer *maskLayer;
@property (nonatomic, assign) CGFloat progress;

@end

@implementation GardView


+ (Class)layerClass {
    return [CAGradientLayer class];
}

 Use a horizontal gradient
//CAGradientLayer *layer = (id)[self layer];
//[layer setStartPoint:CGPointMake(0.0, 0.5)];
//[layer setEndPoint:CGPointMake(1.0, 0.5)];
//
 Create colors using hues in +5 increments
//NSMutableArray *colors = [NSMutableArray array];
//for (NSInteger hue = 0; hue <= 360; hue += 5) {
//    
//    UIColor *color;
//    color = [UIColor colorWithHue:1.0 * hue / 360.0
//                       saturation:1.0
//                       brightness:1.0
//                            alpha:1.0];
//    [colors addObject:(id)[color CGColor]];
//}
//
//[layer setColors:[NSArray arrayWithArray:colors]];

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self != nil) {
        
        _maskLayer = [CALayer layer];
        [_maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];
        [_maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
        [self.layer setMask:_maskLayer];
        
        // Use a horizontal gradient
        CAGradientLayer *layer = (id)[self layer];
        [layer setStartPoint:CGPointMake(0.0, 0.5)];
        [layer setEndPoint:CGPointMake(1.0, 0.5)];
        
        // Create colors using hues in +5 increments
        NSMutableArray *colors = [NSMutableArray array];
        for (NSInteger hue = 0; hue <= 360; hue += 5) {
            
            UIColor *color;
            color = [UIColor colorWithHue:1.0 * hue / 360.0
                               saturation:1.0
                               brightness:1.0
                                    alpha:1.0];
            [colors addObject:(id)[color CGColor]]; 
        } 
        [layer setColors:[NSArray arrayWithArray:colors]];
    }
    
    return self;
}

- (void)performAnimation {
    // Move the last color in the array to the front
    // shifting all the other colors.
    CAGradientLayer *layer = (id)[self layer];
    NSMutableArray *mutable = [[layer colors] mutableCopy];
    id lastColor = [mutable lastObject] ;
    [mutable removeLastObject];
    [mutable insertObject:lastColor atIndex:0];

    NSArray *shiftedColors = [NSArray arrayWithArray:mutable];

    
    // Update the colors on the model layer
    [layer setColors:shiftedColors];
    
    // Create an animation to slowly move the gradient left to right.
    CABasicAnimation *animation;
    animation = [CABasicAnimation animationWithKeyPath:@"colors"];
    [animation setToValue:shiftedColors];
    [animation setDuration:0.08];
    [animation setRemovedOnCompletion:YES];
    [animation setFillMode:kCAFillModeForwards];
    [animation setDelegate:self];
    [layer addAnimation:animation forKey:@"animateGradient"];
}

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {
    [self performAnimation];
}

- (void)setProgress:(CGFloat)value {
    if (_progress != value) {
        // Progress values go from 0.0 to 1.0
        _progress = MIN(1.0, fabs(value));
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    // Resize our mask layer based on the current progress
    CGRect maskRect = [_maskLayer frame];
    maskRect.size.width = CGRectGetWidth([self bounds]) * _progress;
    [_maskLayer setFrame:maskRect];
}

@end



参考文章:

http://www.cocoachina.com/industry/20140705/9039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值