2013-10-22 22 views
1

enter image description here如何設置角的半徑掩膜層

我提出了一個看法。然後我添加了一個這樣的遮罩層。

UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)]; 
dd.backgroundColor = [UIColor redColor]; 

dd.layer.masksToBounds = YES; 
dd.layer.cornerRadius = 30.0f; 
[self.view addSubview:dd]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
CGRect maskRect = CGRectMake(100, 100, 200, 200); 
CGMutablePathRef path = CGPathCreateMutable(); 
CGPathAddRect(path, nil, maskRect); 
[maskLayer setPath:path]; 
CGPathRelease(path); 
dd.layer.mask = maskLayer; 

我該如何在maskLayer上加角?

我試圖

dd.layer.masksToBounds = YES; 
dd.layer.cornerRadius = 30.0f; 

這是行不通的。和其他的東西。

+3

定義固定「不工作」 – dandan78

+0

感謝。 –

+0

@ dandan78定義「不起作用」??是指?? –

回答

3

任何方式,可能是你想這樣的:

enter image description here

添加QuartzCore框架在你的項目

//導入在.m文件

#import <QuartzCore/QuartzCore.h> 

//執行此操作代碼

UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)]; 
dd.backgroundColor = [UIColor redColor]; 

dd.layer.masksToBounds = YES; 
dd.layer.borderColor = [UIColor whiteColor].CGColor; 
[self.view addSubview:dd]; 

CGFloat cornerRadius = 0; 
CGFloat borderWidth = 2; 

UIColor *lineColor = [UIColor orangeColor]; 
CGRect maskRect = CGRectMake(100, 100, 200, 200); 

//drawing 
CGRect frame =maskRect; 

CAShapeLayer *_shapeLayer = [CAShapeLayer layer]; 
//creating a path 
CGMutablePathRef path = CGPathCreateMutable(); 

//drawing a border around a view 
CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius); 
CGPathAddLineToPoint(path, NULL, 0, cornerRadius); 
CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO); 
CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0); 
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO); 
CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius); 
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO); 
CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height); 
CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO); 

//path is set as the _shapeLayer object's path 
_shapeLayer.path = path; 
CGPathRelease(path); 

_shapeLayer.backgroundColor = [[UIColor clearColor] CGColor]; 
_shapeLayer.frame = frame; 
_shapeLayer.masksToBounds = NO; 
_shapeLayer.fillColor = [[UIColor grayColor] CGColor]; 
_shapeLayer.strokeColor = [lineColor CGColor]; 
_shapeLayer.lineWidth = borderWidth; 

//_shapeLayer is added as a sublayer of the view, the border is visible 
[dd.layer addSublayer:_shapeLayer]; 
dd.layer.cornerRadius = cornerRadius; 

可能會起作用。

快樂編碼。

+0

已經添加了QuartzCore。你不明白我需要什麼。 –

+0

你可以顯示任何你想要的截圖嗎? –

+0

@大衛上面的答案掩蓋了你在問題中提出的角落半徑。重新陳述你的問題,如果這不是你正在尋找的答案。 – JSA986

1

您正在從路徑中創建一個圖層,如我所見。因此,圓角應該已經在路徑上起作用了。嘗試使用不只是直接,但貝齊爾曲線。 從矩形這樣創建走投無路半徑UIBezierPath

+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; 

,並用它爲你的圖層路徑

相關問題