2014-05-08 135 views

回答

0

您需要:

  1. 創建CAShapeLayer
  2. 設置它的路徑是基於view.bounds但只有兩個圓角的CGPathRef(可能通過使用[UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:]
  3. 設置你的view.layer .mask成爲CAShapeLayer

只要考慮到這會對性能產生不良影響。 This might help

0

你可以用戶CALayer相同。

CALayer *l = [_btn layer]; 
[l setMasksToBounds:YES]; 
[l setCornerRadius:8.0]; 

,並添加QuartzCore.framework

0

你可能想繼承UIView並重寫drawRect:方法。然後將您的圖像視圖添加爲子視圖。你drawRect:方法會是這個樣子(與clipsToBounds設置爲YES):

- (void)drawRect:(CGRect)rect 
{ 
    UIColor *fillColor = [UIColor clearColor]; 
    UIColor *borderColor = [UIColor redColor]; 
    float borderWidth = 2.0f; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, fillColor.CGColor); 
    CGContextSetLineWidth(context, borderWidth); 
    CGContextSetStrokeColorWithColor(context, borderColor.CGColor); 

    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect)); 
    CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect), rect.size.height/2, 2*M_PI, M_PI, 1); 
    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathFillStroke); 
} 

或者你可以看看與半圓圖像屏蔽或創建圖像視圖CAShapeLayer