直接上代码
//// UIView+FlashesMask.h// RunningLightAnimation
//// Created by iFens on 16/3/29.// Copyright © 2016年 Ifans. All rights reserved.
//#import <UIKit/UIKit.h>@interface UIView (FlashesMask)
-(void)FlashesMaskAimationBeginWithRepeatCount:(CGFloat)count duration:(NSTimeInterval)duration FlashesWidth:(CGFloat)FlashesWidth ;
-(void)FlashesMaskAimationStop;
@end
//// UIView+FlashesMask.m// RunningLightAnimation
//// Created by iFens on 16/3/29.// Copyright © 2016年 Ifans. All rights reserved.
//#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:0.1].CGColor,(id)[UIColor colorWithWhite:0 alpha:1].CGColor,(id)[UIColor colorWithWhite:1 alpha:0.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