做一个类似于iPhone经典滑动解锁动画的发光按钮(流光)代码如下
//device screen size
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define kColorTextDarkGray RGB(100,100,100) //稍深的灰色字体
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong) CAGradientLayer *gradientLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initView];
}
-(void)initView{
UIView *tempView = [UIView new];
[self.view addSubview:tempView];
tempView.backgroundColor = [UIColor grayColor];
tempView.frame = CGRectMake(16, 60, kScreenWidth-32, 44);
UILabel *skipLabel = [[UILabel alloc] init];
skipLabel.font = [UIFont systemFontOfSize:18];
skipLabel.text = @"跳过 > ";
skipLabel.textAlignment = NSTextAlignmentCenter;
skipLabel.textColor = kColorTextDarkGray;
// skipLabel.backgroundColor = [UIColor greenColor];
skipLabel.frame = tempView.bounds;
[tempView addSubview:skipLabel];
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
self.gradientLayer = gradientLayer;
gradientLayer.bounds = skipLabel.bounds;
gradientLayer.position = skipLabel.center;
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);
NSMutableArray *colors = [NSMutableArray array];
[colors addObject:(id)[UIColor blackColor].CGColor];
[colors addObject:(id)[UIColor whiteColor].CGColor];
[colors addObject:(id)[UIColor blackColor].CGColor];
gradientLayer.colors = colors;
gradientLayer.locations = @[@0.2, @0.5, @0.8];
[tempView.layer addSublayer:gradientLayer];
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"locations"];
anim.fromValue = @[@0, @0, @0.3];
anim.toValue = @[@0.7,@1,@1];
anim.duration = 1.5;
anim.repeatCount = HUGE;
anim.removedOnCompletion = NO;
[gradientLayer addAnimation:anim forKey:nil];
gradientLayer.mask = skipLabel.layer;
gradientLayer.masksToBounds = YES;
UIButton *skipBtn = [[UIButton alloc] init];
skipBtn.backgroundColor = [UIColor clearColor];
[skipBtn addTarget:self action:@selector(skipBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[tempView addSubview:skipBtn];
skipBtn.frame = tempView.bounds;
}
-(void)skipBtnClicked{
NSLog(@"---点击我了---");
}