2014-03-27 99 views
0

我將角半徑應用於UIView,即UIRectCornerTopLeftUIRectCornerTopRight。當我申請這個時,邊界在角落裏消失了。如何避免這種情況?如何製作帶有可選圓角和邊框的UIView?

這是我如何適用邊界UIView

[self.middleView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(4, 4)]; 
self.middleView.layer.borderWidth = 0.5f; 
self.middleView.layer.borderColor = [[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.25] 

這是我使用的應用可選圓角的類別:

#import "UIView+Roundify.h" 

    @implementation UIView (Roundify) 
    - (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii { 
    CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii]; 
    self.layer.mask = tMaskLayer; 
    } 

    - (CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii { 
    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = self.bounds; 

    UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect: 
          maskLayer.bounds byRoundingCorners:corners cornerRadii:radii]; 
    maskLayer.fillColor = [[UIColor whiteColor] CGColor]; 
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor]; 
    maskLayer.path = [roundedPath CGPath]; 

    return maskLayer; 
} 
+0

你的意思是可選的圓角和邊框? –

+0

@icodes嘗試將'strokeColor'設置爲'CAShapeLayer * maskLayer' – Akhilrajtr

+0

請參閱此處的答案:http://stackoverflow.com/questions/12756928/cashapelayer-with-border-and-fill-color-and-rounding – n00bProgrammer

回答

1

除非有我們」一些具體要求不知道,如果你所要做的只是繞過角落並有邊界,那麼貝塞爾路徑和掩碼是不必要的。我通常會這樣做: myView.layer.borderWidth = 2; myView.layer.cornerRadius = 5;

難道你只是想要四捨五入的頂角,你需要不使用層舍入?如果是這樣,爲什麼不使用它,然後覆蓋一個薄的視圖來覆蓋底部位?有點煩瑣,但是我發現越是靠標準控件來繪製自己,而不是進入核心圖形就越好。

編輯:好的,因爲它需要有底角不是圓的,怎麼樣,如果你有2子視圖上的UIView類別:1與4圓角和另一奠定過頂(個體經營bringSubviewToFront)它簡單地覆蓋了帶有非圓形條帶的圓形視圖的「頁腳」,即具有等於圓角半徑的等寬和小高度的視圖。如果你有一個純色背景,那麼就讓兩個子視圖都一樣;如果您有一些紋理或圖像背景,請使它們都透明,並將紋理/圖像放在超級視圖(使用您類別的特定佈局方法的父視圖)上。最後,將邊框放在同一個超級視圖上。應該工作,讓我知道你的想法。

+0

是的,我的要求是特定的。我在我的應用程序的幾個視圖中使用了這個類別,只有頂部兩個角被邊框圓化。我可以得到圓角但不能將邊框應用到這些邊框 – icodes

+0

請參閱已編輯的答案 – bobsmells

4

下面的代碼嘗試工作

您的看法要圓左上和TopRight

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; 
    [view1 setBackgroundColor:[UIColor grayColor]]; 
    [self.view addSubview:view1]; 

組邊角如下代碼

UIBezierPath *maskPath; 
maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.frame = self.view.bounds; 
maskLayer.path = maskPath.CGPath; 
view1.layer.mask = maskLayer; 

輸出爲:

enter image description here

+0

我可以得到圓角,但無法將邊界應用於該視圖。邊界在角落消失 – icodes

1

找到了這段代碼。實際上並沒有嘗試過,但似乎是你需要的。

- (void)drawDashedBorderAroundView:(UIView *)v { 

    //border definitions 
    CGFloat cornerRadius = 10; 
    CGFloat borderWidth = 2; 
    NSInteger dashPattern1 = 8; 
    NSInteger dashPattern2 = 8; 
    UIColor *lineColor = [UIColor orangeColor]; 

    //drawing 
    CGRect frame = v.bounds; 

    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 setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"]; 
    _shapeLayer.fillColor = [[UIColor clearColor] CGColor]; 
    _shapeLayer.strokeColor = [lineColor CGColor]; 
    _shapeLayer.lineWidth = borderWidth; 
    _shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern1], [NSNumber numberWithInt:dashPattern2], nil]; 
    _shapeLayer.lineCap = kCALineCapRound; 

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

這段代碼增加了一條虛線,但您可以通過_shapeLayer.lineDashPattern修改該代碼。