iOS 流光特效 呼吸效果

效果

思路

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;

/** 效果持续时间 */
@property (nonatomic, assign) CGFloat flowLightDuration;
/** 效果间隔时间 */
@property (nonatomic, assign) NSTimeInterval flowLightInternal;

/** 呼吸变化幅度,默认0.04 */
@property (nonatomic, assign) CGFloat breathRange;
/** 效果持续时间 */
@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> 效果卡住需重启:呼吸效果离开可视区域(滚出可视区域、切换前后台、进入子页)会卡住,重现时需重启。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToResetEffect) name:Notification_ShouldToResetBtnBreathEffect object:nil];

- (void)respondToResetEffect {
    [_btn breathUninstall];
    [_btn breathInstall];
}

2> List添加多个流光特效,要求同时触发:添加timer,统一触发流光效果

[[NSNotificationCenter defaultCenter] addObserverForName:NotificationName_TriggerFlowLightEffectDisposable object:nil queue:nil usingBlock:^(NSNotification * _Nonnull notification) {
  [weakSelf.btn flowLightDisposable]; // 一次性
}];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容