iOS 闪光的按钮,流光动画,iPhone经典滑动解锁动画

做一个类似于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,strongCAGradientLayer *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(@"---点击我了---");

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值