【发布时间】:2014-09-08 12:27:20
【问题描述】:
我需要实现带有圆孔的模糊视图,这将在 iOS7+ 中工作
我尝试使用本机 UIToolbar,但在 iOS7 中出现一些奇怪的行为,当我将工具栏半透明设置为 YES 并应用蒙版时,它会消失。当我尝试在 iOS8 中做同样的事情时,它工作正常。
叠加视图的init方法代码:
- (instancetype)initWithFrame:(CGRect)frame withTransparentCircleInCenet:(CGPoint)center radius:(CGFloat)radius withStrokeSize:(CGFloat)strokeSize andColor:(CGColorRef)strokeColor
{
self = [super initWithFrame:frame];
if (self) {
self.opaque = NO;
//Use UIToolbar for Blur
_nativeBlurView = [[UIToolbar alloc] initWithFrame:self.bounds];
_nativeBlurView.barStyle = UIBarStyleBlack;
_nativeBlurView.translucent = YES;
[self.layer insertSublayer:_nativeBlurView.layer atIndex:0];
//Create mask with hole
CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.frame = _nativeBlurView.layer.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:mask.frame];
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO];
[maskPath appendPath:circlePath];
[maskPath setUsesEvenOddFillRule:YES];
mask.path = maskPath.CGPath;
[mask setFillRule:kCAFillRuleEvenOdd];
mask.fillColor = [[UIColor blackColor] CGColor];
_nativeBlurView.layer.mask = mask;
//draw circle over the hole
UIBezierPath *strokeCirclePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO];
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [strokeCirclePath CGPath];
circle.strokeStart = 0;
circle.strokeEnd = 1;
circle.lineWidth = strokeSize;
circle.strokeColor = strokeColor;
circle.fillColor = [[UIColor clearColor] CGColor];
circle.shadowOffset = CGSizeMake(0, strokeSize);
circle.shadowOpacity = 0.5;
circle.shadowColor = [[UIColor blackColor] CGColor];
[self.layer insertSublayer:circle atIndex:1];
}
return self;
}
截图:
iOS7:
iOS8:
【问题讨论】:
标签: ios7 uiview ios8 uitoolbar blur