【问题标题】:How to Draw the Gaussian Blur Effect?如何绘制高斯模糊效果?
【发布时间】:2019-05-30 08:43:42
【问题描述】:

我需要使用 Objective C 在 UIView 上绘制模糊效果。

这是我的设计

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle: UIBlurEffectStyleExtraLight];

    UIVisualEffectView *contentView = [[UIVisualEffectView alloc] initWithEffect: blurEffect];

    contentView.layer.cornerRadius = 18.0;

    contentView.layer.masksToBounds = YES;

    contentView.backgroundColor = [UIColor whiteColor];

    contentView.userInteractionEnabled = NO;

    contentView.layer.shadowRadius = 10;

    contentView.layer.shadowOpacity = 0.2f;

    contentView.layer.shadowColor = [UIColor whiteColor].CGColor;

    contentView.layer.shadowOffset =  CGSizeZero;

    [contentView setFrame: annotationFrame];

    [contentView setTranslatesAutoresizingMaskIntoConstraints: YES];

    self.contentView = contentView;

下面的截图是使用上面代码的当前输出

我已经尝试了上面的代码。 ,但我没有得到确切的模糊效果 请建议我画这个设计。

我已经更新了下面的代码

- (void)drawRect:(CGRect)rect {

    [super drawRect: rect]; //i doubt we need a super drarRect here…

    [self drawMarker: self.markerView fillColor: [UIColor whiteColor]];

    CGContextRef ctx = UIGraphicsGetCurrentContext();


    CGPathRef path = CGPathCreateWithRoundedRect(CGRectInset(rect,  self.frame.size.width/4,  self.frame.size.height/4), self.frame.size.width/8, self.frame.size.height/8, nil);

    CGContextSetFillColorWithColor(ctx,[UIColor slateColor].CGColor);

    CGContextSetShadowWithColor(ctx, CGSizeZero, self.frame.size.height/4,  [UIColor slateColor].CGColor);


    for ( int i = 0; i<=6; i++) {
        CGContextAddPath(ctx, path);
        CGContextFillPath(ctx);
    }

}

我需要视图背后的模糊效果,请就此向我提出建议。

谢谢。

【问题讨论】:

  • 检查这个stackoverflow.com/questions/37745673/…也许可以帮助你,让我知道
  • 我只会崩溃
  • 崩溃或错误?这段代码有 2 年的历史了,无论如何我都可以更新它,顺便说一句,这个答案很快

标签: ios objective-c xcode gaussianblur


【解决方案1】:

将我之前的答案从 swift 转换为 Objective-C 我们有这个

@implementation UICustomBackgroundButton


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGPathRef path = CGPathCreateWithRoundedRect(CGRectInset(rect,  self.frame.size.width/4,  self.frame.size.height/4), self.frame.size.width/8, self.frame.size.height/8, nil);

    CGContextSetFillColorWithColor(ctx,[UIColor blueColor].CGColor);
    CGContextSetShadowWithColor(ctx, CGSizeZero, self.frame.size.height/4,  [UIColor blueColor].CGColor);


    for ( int i = 0; i<=6; i++) {
        CGContextAddPath(ctx, path);
        CGContextFillPath(ctx);
    }
}


@end

结果

【讨论】:

  • #reinier-melian 我添加了代码,但我没有得到确切的模糊位置请检查我的问题我更新了新代码和屏幕截图
  • @Nandhakumar 我会检查并告诉你
【解决方案2】:

查看以下我用于模糊效果的参考:

https://dzone.com/articles/applying-gaussian-blur-to-uiviews-with-swift-proto

这仅适用于高斯模糊效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 2010-11-17
    • 1970-01-01
    • 2020-03-20
    • 2012-10-23
    • 1970-01-01
    • 2014-03-28
    • 2021-10-02
    相关资源
    最近更新 更多