http://www.tuicool.com/articles/RZBFBn
http://www.devtalking.com/articles/calayer-animation-gradient-animation/
直接粘贴代码了:OC的,上边有swift的:
//
// ViewController.m
// oc
//
// Created by mudy on 15/11/2.
// Copyright © 2015年 mudy. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UIView *backgroundView;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;
@property (nonatomic,strong)NSString *text;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.gradientLayer = [[CAGradientLayer alloc]init];
self.text = @"梦想还是要有的,万一实现了呢";
self.gradientLayer.bounds = CGRectMake(0, 0, self.backgroundView.frame.size.width, self.backgroundView.frame.size.height);
self.gradientLayer.position = CGPointMake(self.backgroundView.frame.size.width/2, self.backgroundView.frame.size.height/2);
self.gradientLayer.startPoint = CGPointMake(0, 0.5);
self.gradientLayer.endPoint = CGPointMake(1, 0.5);
self.gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor yellowColor].CGColor,
(__bridge id)[UIColor redColor].CGColor];
self.gradientLayer.locations = @[@0.2, @0.5, @0.8];
[self.backgroundView.layer addSublayer:self.gradientLayer];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
self.textLabel.text = self.text;
self.gradientLayer.mask = self.textLabel.layer;
[self gradinetAnimate];
}
-(void)gradinetAnimate{
CABasicAnimation *gradient = [CABasicAnimation animationWithKeyPath:@"locations"];
// let gradient = CABasicAnimation(keyPath: "locations")
gradient.fromValue = @[@0, @0, @0.25];
gradient.toValue = @[@0.75, @1, @1];
gradient.duration = 4.5;
gradient.repeatCount = HUGE;
[self.gradientLayer addAnimation:gradient forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end