ios - 如何绘制高斯模糊效果?
问题描述
我需要使用 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);
}
}
我需要视图背后的模糊效果,请就此向我提出建议。
谢谢你。
解决方案
将我之前的答案从 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
推荐阅读
- asp.net-core - 如何通过值转换修改属性上的 EntityState?
- sql - 如何将日期从 YYYY-MM-DD 转换/更新为 DDMMMYYYY 格式?
- python - 无法使用 Dask 从 HDFS 读取 (read_csv) (FileNotFoundError: [Errno 2])
- bit-manipulation - 为什么 INT_MAX & x = x?
- java - Lombok - java.lang.StackOverflowError:toString 方法上的 null
- windows - Web 应用程序 Kerberos 身份验证:与 cookie 结合的正确方法是什么?
- c# - 如何在 ArgumentException 中根据条件添加更多变量
- openid-connect - 如何允许未经授权的用户使用具有 OpenID Connect 身份验证类型的 AppSync 访问某些查询/突变?
- html - 最后包含静态 CSS 文件(用于品牌推广)
- javascript - JavaScript 动画未与 HTML 链接