笔记效果图
创建UIView的Category
#import <UIKit/UIKit.h>
@interface UIView (FlashesMask)
/**
开始
@param count 循环次数
@param duration 单次时间
@param FlashesWidth 光圈宽度
*/
-(void)flashesMaskAimationBeginWithRepeatCount:(CGFloat)count duration:(NSTimeInterval)duration FlashesWidth:(CGFloat)FlashesWidth;
/**
结束
*/
-(void)flashesMaskAimationStop;
@end
#import "UIView+FlashesMask.h"
#define FlashesMaskAinmation @"FlashesMaskAinmation"
@implementation UIView (FlashesMask)
-(void)flashesMaskAimationBeginWithRepeatCount:(CGFloat)count duration:(NSTimeInterval)duration FlashesWidth:(CGFloat)FlashesWidth
{
NSAssert([self isKindOfClass:[UIView class]], @"必须是UIView子类");
CAGradientLayer *FlashesMaskLayer = [[CAGradientLayer alloc]init];
[FlashesMaskLayer setFrame:self.bounds];
CGFloat flashSize = FlashesWidth / self.frame.size.width;
NSArray *startLocations = @[@0,
[NSNumber numberWithFloat:(flashSize / 2.)],
[NSNumber numberWithFloat:flashSize]
];
NSArray *endLocations = @[[NSNumber numberWithFloat:(1. - flashSize)],
[NSNumber numberWithFloat:(1. - (flashSize / 2.))],
@1
];
FlashesMaskLayer.locations = startLocations;
FlashesMaskLayer.startPoint = CGPointMake(0 - (flashSize * 2), .5);
FlashesMaskLayer.endPoint = CGPointMake(1 + flashSize, .5);
[FlashesMaskLayer setColors:@[(id)[UIColor colorWithWhite:1 alpha:1].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.1].CGColor,(id)[UIColor colorWithWhite:1 alpha:1].CGColor]];
CABasicAnimation * base = [CABasicAnimation animationWithKeyPath:@"locations"];
base.fromValue = startLocations;
base.toValue = endLocations;
[base setDuration:duration];
[base setRepeatCount:count];
self.layer.mask=FlashesMaskLayer;
[FlashesMaskLayer addAnimation:base forKey:FlashesMaskAinmation];
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if ([anim valueForKey:FlashesMaskAinmation]) {
self.layer.mask = nil;
}
}
-(void)flashesMaskAimationStop{
[self.layer.mask removeAnimationForKey:FlashesMaskAinmation];
}
@end
使用
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
label.text = @"小的时候上学老师总是说你比不过我我也躲在角落里偷偷笑过";
label.font = [UIFont boldSystemFontOfSize:18];
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
[label flashesMaskAimationBeginWithRepeatCount: HUGE_VALF duration:0.8 FlashesWidth:30];