有时候UI给的图里面用到了模糊效果,但是官方的UIEffectView的模糊值太重,想要控制其模糊程度的话,就需要配合UIViewPropertyAnimator使用。
头文件CustomEffectView.h
@interface CustomEffectView : UIVisualEffectView
@property (nonatomic, assign) CGFloat intensity;
@property (nonatomic, strong) UIViewPropertyAnimator * animator;
- (instancetype)initWithEffect:(UIVisualEffect *)effect intensity:(CGFloat)intensity;
@end
CustomEffectView.m
@implementation CustomEffectView
- (instancetype)initWithEffect:(UIVisualEffect *)effect intensity:(CGFloat)intensity {
self = [super initWithEffect:nil];
if (self) {
self.effect = nil;
self.intensity = intensity;
self.animator = [[UIViewPropertyAnimator alloc] initWithDuration:1 curve:UIViewAnimationCurveLinear animations:^{
self.effect = effect;
}];
self.animator.fractionComplete = self.intensity;
if (@available(iOS 11.0, *)) {
self.animator.pausesOnCompletion = YES;
}
}
return self;
}
@end
使用方法
使用initWithEffect:intensity:方法初始化,并在viewWillAppear方法内调用setEffectView