iOS 开发中一般有系统的UIVisualEffectView处理模糊效果,但是其模糊程度不好控制,很难到达想要的效果。不过我们可以采用 UIViewPropertyAnimator 实现该要求。
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
self.imageView.image = [UIImage imageNamed:@"test.jpg"];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.imageView];
self.effectView = [[UIVisualEffectView alloc]initWithFrame:self.view.bounds];
self.effectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
[self.view addSubview:self.effectView];
self.animator = [[UIViewPropertyAnimator alloc] initWithDuration:0.1 curve:UIViewAnimationCurveLinear animations:^{
self.effectView.effect = nil;
}];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 300, 300, 30)];
[slider addTarget:self action:@selector(sliderValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
}
- (void)sliderValueDidChange:(UISlider *)slider
{
self.animator.fractionComplete = slider.value;
}