Less Blur with `Visual Effect View with Blur`?

后端 未结 11 1315
死守一世寂寞
死守一世寂寞 2020-12-08 10:11

Title pretty much asks it all...

I\'m playing with the iOS8 Visual Effect View with Blur. It\'s over a UIImageView that shows a user choosa

11条回答
  •  不思量自难忘°
    2020-12-08 11:02

    I use UIVisualEffectView like this to get adjustable blur circles. The blur level is controlled by a slider that controls the alpha. I'll include the slider handler below too. The blur circle size is adjustable with pinch spread action. I will include that too. And you can drag around the blur circles. I'll leave that as an exercise for the reader. If you want a blur rectangle, just don't round the corners. To see this blur circle design in action, load the MemeSoEasy app (free), add a photo (that you can put a blur circle on top of), then add a blur circle.

    UIVisualEffectView *blurVisualEffectView;
    
    UIVisualEffect *blurEffect;
    blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    blurVisualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurVisualEffectView.frame = lastChosenBlurCircleRect;
    blurVisualEffectView.center = CGPointMake(halfScreenX, halfScreenY);
    [self.view addSubview:blurVisualEffectView];
    CGFloat blurCornerRadius = blurVisualEffectView.bounds.size.width/2;
    [[blurVisualEffectView layer]setCornerRadius:blurCornerRadius];
    [[blurVisualEffectView layer]setMasksToBounds:YES];
    [[blurVisualEffectView layer] setBorderWidth:4.0f];
    [[blurVisualEffectView layer] setBorderColor:[UIColor blueColor].CGColor];
    blurVisualEffectView.userInteractionEnabled = NO;
    blurVisualEffectView.alpha = 0.97;
    [blurArray addObject:blurVisualEffectView];
    

    Slider handler :

    Note that I store my blur objects in an array, so I can let users create as many as desired. The slider handler works on the last object in the array. The slider min and max values are 0.0 and 1.0

    UISlider *slider_ = (UISlider *)sender;
    CGFloat ourSliderValue = slider_.value;
    UIVisualEffectView *currentBlurObject =
    [blurArray objectAtIndex:blurArray.count - 1];
    currentBlurObject.alpha = ourSliderValue;
    

    Size change handler for pinch spread

    int changeInWidth = 0; // one pixel at a time
    
    if (pinchGesture.scale > 1.0) {
        changeInWidth++;
    }
    if (pinchGesture.scale < 1.0) {
        changeInWidth--;
    }
    
    UIVisualEffectView *currentBlurObject =
    [blurArray objectAtIndex:blurArray.count - 1];
    
    CGPoint oldCenter = currentBlurObject.center;
    
    currentBlurObject.frame = CGRectMake(0, 0, currentBlurObject.frame.size.width + changeInWidth, currentBlurObject.frame.size.width + changeInWidth);
    
    currentBlurObject.center = oldCenter;
    
    lastChosenBlurCircleRect = currentBlurObject.frame;
    
    CGFloat blurCornerRadius = currentBlurObject.frame.size.width/2;
    [[currentBlurObject layer]setCornerRadius:blurCornerRadius];
    

提交回复
热议问题