效果

思路
1> 流光:由CAGradientLayer繪制,或直接找UI要圖
2> 動效的時間片計算:animation 的 keyTimes取值范圍 0~1,要計算時間片比例,duration時間片比例為 effectDuration / (effectDuration + effectInterval)
功能代碼
@interface FlowLightButton : UIButton
- (void)flowLightInstall;
- (void)flowLightUninstall;
- (void)flowLightDisposable;
- (void)breathInstall;
- (void)breathUninstall;
/** 效果持續(xù)時間 */
@property (nonatomic, assign) CGFloat flowLightDuration;
/** 效果間隔時間 */
@property (nonatomic, assign) NSTimeInterval flowLightInternal;
/** 呼吸變化幅度,默認(rèn)0.04 */
@property (nonatomic, assign) CGFloat breathRange;
/** 效果持續(xù)時間 */
@property (nonatomic, assign) CGFloat breathDuration;
@end
#import "FlowLightButton.h"
#import <Masonry.h>
#define kFlowLightAnimationKey @"kFlowLightAnimationKey"
#define kBreathAnimationKey @"kBreathAnimationKey"
@interface FreshFlowLightButton ()
@property (nonatomic, assign) BOOL flowLightSetted;
@property (nonatomic, assign) BOOL breathSetted;
@property (nonatomic, weak) CALayer *gradientLayer;
@end
@implementation FreshFlowLightButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.flowLightSetted = NO;
self.breathSetted = NO;
}
return self;
}
- (void)flowLightInstall {
if (self.flowLightSetted) return;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self flowLightEffectWithRepeatTimes:CGFLOAT_MAX];
self.flowLightSetted = YES;
});
}
- (void)flowLightDisposable {
[self flowLightEffectWithRepeatTimes:1];
}
- (void)flowLightEffectWithRepeatTimes:(CGFloat)repeatTimes {
CAGradientLayer *gradient = [CAGradientLayer layer];
CGColorRef middleRef = [UIColor.whiteColor colorWithAlphaComponent:0.6].CGColor;
CGColorRef fadeRef = [UIColor.whiteColor colorWithAlphaComponent:0].CGColor;
gradient.colors = @[(__bridge id)fadeRef, (__bridge id)middleRef, (__bridge id)fadeRef];
gradient.locations = @[@0.6, @0.98, @1];
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 0.09);
CGRect rect = self.bounds;
rect.size.width = self.bounds.size.width + 100;
gradient.frame = rect;
[self.layer addSublayer:gradient];
self.gradientLayer = gradient;
CGFloat startX = -gradient.frame.size.width;
CGFloat endX = 100;
CAKeyframeAnimation *flowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
NSTimeInterval duration = self.flowLightDuration ?: 1;
NSTimeInterval interval = self.flowLightInternal ?: 1;
CGFloat durationKeyTimes = duration / (duration + interval);
flowAnimation.values = @[@(startX), @(endX), @(endX)];
flowAnimation.duration = duration + interval;
flowAnimation.keyTimes = @[@0, @(durationKeyTimes), @1];
flowAnimation.repeatCount = repeatTimes;
flowAnimation.removedOnCompletion = repeatTimes == 1 ? YES: NO;
flowAnimation.fillMode = kCAFillModeForwards;
[gradient addAnimation:flowAnimation forKey:kFlowLightAnimationKey];
}
- (void)flowLightUninstall {
[self.gradientLayer removeAnimationForKey:kFlowLightAnimationKey];
[self.gradientLayer removeFromSuperlayer];
self.flowLightSetted = NO;
}
- (void)breathInstall {
if (self.breathSetted) return;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
CGFloat min = 1 - (self.breathRange ?: 0.04);
CGFloat duration = self.breathDuration ?: 1;
scaleAnimation.values = @[@1, @(min), @1];
scaleAnimation.duration = duration;
scaleAnimation.repeatCount = CGFLOAT_MAX;
[self.layer addAnimation:scaleAnimation forKey:kBreathAnimationKey];
self.breathSetted = YES;
});
}
- (void)breathUninstall {
[self.layer removeAnimationForKey:kBreathAnimationKey];
self.breathSetted = NO;
}
遇到的問題
1> 效果卡住需重啟:呼吸效果離開可視區(qū)域(滾出可視區(qū)域、切換前后臺、進入子頁)會卡住,重現(xiàn)時需重啟。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToResetEffect) name:Notification_ShouldToResetBtnBreathEffect object:nil];
- (void)respondToResetEffect {
[_btn breathUninstall];
[_btn breathInstall];
}
2> List添加多個流光特效,要求同時觸發(fā):添加timer,統(tǒng)一觸發(fā)流光效果
[[NSNotificationCenter defaultCenter] addObserverForName:NotificationName_TriggerFlowLightEffectDisposable object:nil queue:nil usingBlock:^(NSNotification * _Nonnull notification) {
[weakSelf.btn flowLightDisposable]; // 一次性
}];