美文网首页iOS相关
使用UIViewPropertyAnimator 来控制毛玻璃

使用UIViewPropertyAnimator 来控制毛玻璃

作者: 大哈哈哈哈 | 来源:发表于2018-08-09 11:08 被阅读53次

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


QQ20180809-110607.gif

相关文章

网友评论

    本文标题:使用UIViewPropertyAnimator 来控制毛玻璃

    本文链接:https://www.haomeiwen.com/subject/uusqbftx.html