iOS 流光特效

效果

思路

1> 流光:由CAGradientLayer绘制,或直接找UI要图
2> 动效的时间片计算:animation 的 keyTimes取值范围 0~1,要计算时间片比例,duration时间片比例为 effectDuration / (effectDuration + effectInterval)

代码

@interface FlowLightButton : UIButton
- (void)flowLightInstall;
- (void)flowLightUninstall;

- (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 

#define kFlowLightAnimationKey  @"kFlowLightAnimationKey"
#define kBreathAnimationKey     @"kBreathAnimationKey"

@interface FNFreshFlowLightButton ()

@property (nonatomic, assign) BOOL flowLightSetted;
@property (nonatomic, assign) BOOL breathSetted;
@property (nonatomic, weak) CALayer *gradientLayer;

@end

@implementation FNFreshFlowLightButton

- (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(), ^{
        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 = CGFLOAT_MAX;
        flowAnimation.removedOnCompletion = NO;
        flowAnimation.fillMode = kCAFillModeForwards;
        [gradient addAnimation:flowAnimation forKey:kFlowLightAnimationKey];
        self.flowLightSetted = YES;
    });
}

- (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;
}

你可能感兴趣的:(iOS 流光特效)