2013-04-23 55 views
9

我想爲我的視圖製作圓角並屏蔽奇怪的問題。UIBezierPath不會繪製圓角底部

我用下面的代碼,以使圓角對我的看法

bezierPath = [UIBezierPath bezierPathWithRoundedRect:btnView.bounds 
             byRoundingCorners:UIRectCornerAllCorners 
              cornerRadii:radius]; 
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.frame = btnView.bounds; 
    maskLayer.path = bezierPath.CGPath; 
    btnView.layer.mask = maskLayer; 

的問題是,它不工作的底角。如果我使用UIRectCornerBottomLeft什麼也沒有發生,如果我使用UIRectCornerAllCorners只有頂角四捨五入。

編輯:我不使用layer.cornerRadius,因爲我只是想圓底角。

原來我沒有問題,如果我從我的視圖中刪除UIViewAutoresizingFlexibleHeight自動調整掩碼。但我想使用自動調整掩碼。如果是的話,可以嗎?

我已經嘗試在設置圖層蒙版之前和之後設置autoresizingMask。沒有運氣!

+1

爲什麼不使用layer.cornerradius? – 2013-04-23 09:09:38

+0

right @ LithuT.V我發表了那個答案,然後再發表評論:) – 2013-04-23 09:11:04

+0

@ LithuT.V我已更新我的問題。 – Mert 2013-04-23 09:17:42

回答

3

,直到我得到的另一個答案,如果是可以使用autoresizingMask與UIBezierPath,我說NO並標記爲正確答案。

我不會刪除我的問題,因爲任何人都可以在不知道原因的情況下屏蔽相同的問題。

+0

只是儘量在 dispatch_async代碼(dispatch_get_main_queue()^ {// 使圓角 }); – do01 2016-11-14 14:18:32

1

的則不是爲圓角的是使用婁代碼...

btnView.layer.cornerRadius = 8.0; 

只是在你的.m像波紋管導入QuartzCore/QuartzCore.h ...

#import <QuartzCore/QuartzCore.h> 

UPDATE:

爲此,您可以使用不同的RoundingCornersUIBezierPath屬性llow ....

對於防爆:UIRectCornerBottomLeftUIRectCornerBottomRightUIRectCornerTopLeftUIRectCornerTopRightUIRectCornerAllCorners

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: btnView.bounds 
               byRoundingCorners:UIRectCornerBottomLeft 
                cornerRadii:CGSizeMake(10.0, 10.0)]; 

CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.frame = btnView.bounds; 
maskLayer.path = maskPath.CGPath; 
btnView.layer.mask = maskLayer; 
[btnView setNeedsDisplay]; 
+0

我已更新我的問題。 – Mert 2013-04-23 09:18:51

+0

@Mert也看到這個鏈接與許多答案.. http://stackoverflow.com/questions/2264083/rounded-uiview-using-calayers-only-some-corners-how – 2013-04-23 09:28:31

+0

@Mert add [btnView setNeedsDisplay];最後... – 2013-04-23 09:37:14

0

你可以用戶view.layer.cornerRadius設置圓角。

編輯:使用以下

layer.mask = MTDContextCreateRoundedMask(layer.bounds, topleftRadius, topRightRadius, bottomLeftRadius,bottomRightRadius); 

這是支持的方法。

static inline UIImage* MTDContextCreateRoundedMask(CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br) 
{ 
    CGContextRef context; 
    CGColorSpaceRef colorSpace; 

    colorSpace = CGColorSpaceCreateDeviceRGB(); 

    context = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 

    CGColorSpaceRelease(colorSpace); 

    if (context == NULL) { 
     return NULL; 
    } 


    CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect); 
    CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect); 

    CGContextBeginPath(context); 
    CGContextSetGrayFillColor(context, 1.0, 0.0); 
    CGContextAddRect(context, rect); 
    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathFill); 

    CGContextSetGrayFillColor(context, 1.0, 1.0); 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, minx, midy); 
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius_bl); 
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius_br); 
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius_tr); 
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius_tl); 
    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathFill); 

    CGImageRef bitmapContext = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 

    UIImage *theImage = [UIImage imageWithCGImage:bitmapContext]; 
    CGImageRelease(bitmapContext); 

    return theImage.layer.mask; 
} 
+0

我已經更新了我的問題。 – Mert 2013-04-23 09:19:13

+0

我得到「函數的隱式聲明MTDContextCreateRoundedMask無效」。我找不到任何關於該功能的文檔是否是一個私有API? – Mert 2013-04-23 09:47:33

+0

我得到警告「不兼容的指針類型從的UIImage *分配給CALayer的*」如果我運行它崩潰壞訪問 – Mert 2013-04-23 10:06:15

9

我剛剛有一個類似的問題需要數小時解決 - 答案很簡單:將我的代碼從viewDidLoad移動到viewDidLayoutSubviews:這是自動佈局完成後調用的第一個方法....所以我懷疑我做了有4個圓角,但不知何故他們離開了屏幕邊緣。

+0

旋轉viewDidLayoutSubviews不會調用,不適合自動佈局。 – Vahid 2016-11-08 08:50:24

0

解決方案:

-(void)roundCorners:(UIRectCorner)corner radius:(float)radius 
{ 
    _cornerRadius = radius; 
    _corner = corner; 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    [super drawRect:rect]; 

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:_corner cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)]; 

    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = self.bounds; 
    maskLayer.path = maskPath.CGPath; 

    self.layer.mask = maskLayer; 
} 
0

不得不改變視角。由於autoresizing而無法在viewDidLoad中使用底角。必須將代碼移動到viewDidAppear。

-(void)viewDidAppear:(BOOL)animated{ 
[super viewDidAppear:animated]; 

CGRect bounds = self.view.bounds; 
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.0, 5.0)]; 

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

self.view.layer.masksToBounds = YES; 
[self.view setNeedsDisplay]; 
} 
0

最後我得到了成功,它真的有效。嘗試一下。

CGRect frame = self.accountBackgroundImage2.frame; 
frame.size.height = self.view.bounds.size.height; 
UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:self.accountBackgroundImage2.bounds byRoundingCorners:(UIRectCornerBottomLeft |UIRectCornerBottomRight) cornerRadii:CGSizeMake(12.5, 12.5)]; 

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init]; 
shapeLayer.frame = self.accountBackgroundImage2.bounds; 
shapeLayer.path = path.CGPath; 
_accountBackgroundImage2.layer.mask = shapeLayer; 
self.accountBackgroundImage2.frame = frame; 
0

換種方式

在執行 「viewWillLayoutSubviews」

- (void)viewWillLayoutSubviews{ 
[self roundCornersOnView:self.buttonJoin onTopLeft:NO topRight:YES bottomLeft:YES bottomRight:NO radius:10.0]; 
} 

功能

-(UIButton *)roundCornersOnView:(UIButton *)button onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius { 

if (tl || tr || bl || br) { 
    UIRectCorner corner = 0; //holds the corner 
    //Determine which corner(s) should be changed 
    if (tl) { 
     corner = corner | UIRectCornerTopLeft; 
    } 
    if (tr) { 
     corner = corner | UIRectCornerTopRight; 
    } 
    if (bl) { 
     corner = corner | UIRectCornerBottomLeft; 
    } 
    if (br) { 
     corner = corner | UIRectCornerBottomRight; 
    } 

    UIButton *roundedView = button; 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)]; 
    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = roundedView.bounds; 
    maskLayer.path = maskPath.CGPath; 
    roundedView.layer.mask = maskLayer; 
    return roundedView; 
} else { 
    return button; 
} 

}

0

我認爲你應該使用

bezierPath.lineCapStyle = CGLineCap.Round