2016-04-05 20 views
2

我想圓我UIView使用類似值
top-left-radius:20;bottom-right-radius:5;bottom-left-radius:5;top-right-radius:10;IOS:可以圓有不同的價值半徑在每個角落

//For rounder `UIRectCornerBottomLeft & UIRectCornerBottomRight` I use 

    UIBezierPath *maskPath0 = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5.0, 5.0)]; 

    CAShapeLayer *maskLayer0 = [[CAShapeLayer alloc] init]; 
    maskLayer0.frame = self.bounds; 
    maskLayer0.path = maskPath0.CGPath; 
    self.messageView.layer.mask = maskLayer0; 


    //For rounder `TopRight` I use 

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)]; 

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


    //For rounder `TopLeft` I use 

    UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerTopLeft) cornerRadii:CGSizeMake(20.0, 20.0)]; 

    CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init]; 
    maskLayer2.frame = self.bounds; 
    maskLayer2.path = maskPath2.CGPath; 
    self.messageView.layer.mask = maskLayer2; 

但結果我得到的是與視圖拐角半徑爲TopLeft,值爲20. 我該如何實現這個圓角?任何幫助將非常感激。

+0

您需要使用相同的對象添加所有的拐角半徑,截至目前最後一個對象正在應用拐角半徑。 –

+0

@VatsalK你是對的,但現在我不知道把它添加到同一個對象中,你能用代碼來描述它嗎?非常感謝你 –

+0

在這種情況下,你需要按照你的要求繪製形狀,你可以參考這個[link](http://stackoverflow.com/questions/8702696/drawing-smooth-curves-methods-needed)或者您可以製作相同形狀的圖像並將圖像設置爲背景。 –

回答

4

UIBezierPath沒有這樣的方法。但是你可以用一堆addLineToPointaddArcWithCenter方法來做到這一點:

let minx = CGRectGetMinX(rect) 
let miny = CGRectGetMinY(rect) 
let maxx = CGRectGetMaxX(rect) 
let maxy = CGRectGetMaxY(rect) 

let path = UIBezierPath() 
path.moveToPoint(CGPointMake(minx + topLeftRadius, miny)) 
path.addLineToPoint(CGPointMake(maxx - topRightRadius, miny)) 
path.addArcWithCenter(CGPointMake(maxx - topRightRadius, miny + topRightRadius), radius: topRightRadius, startAngle:3 * M_PI_2, endAngle: 0, clockwise: true) 
path.addLineToPoint(CGPointMake(maxx, maxy - bottomRightRadius)) 
path.addArcWithCenter(CGPointMake(maxx - bottomRightRadius, maxy - bottomRightRadius), radius: bottomRightRadius, startAngle: 0, endAngle: M_PI_2, clockwise: true) 
path.addLineToPoint(CGPointMake(minx + bottomLeftRadius, maxy)) 
path.addArcWithCenter(CGPointMake(minx + bottomLeftRadius, maxy - bottomLeftRadius), radius: bottomLeftRadius, startAngle: M_PI_2, endAngle: M_PI, clockwise: true) 
path.addLineToPoint(CGPointMake(minx, miny + topLeftRadius)) 
path.addArcWithCenter(CGPointMake(minx + topLeftRadius, miny + topLeftRadius), radius: topLeftRadius, startAngle: M_PI, endAngle: 3 * M_PI_2, clockwise: true) 
path.closePath() 

對於對象 - C:

CGFloat topLeftRadius = 20; 
CGFloat topRightRadius = 10; 
CGFloat bottomRightRadius = 5; 
CGFloat bottomLeftRadius = 5; 

CGFloat minx = CGRectGetMinX(self.messageView.bounds); 
CGFloat miny = CGRectGetMinY(self.messageView.bounds); 
CGFloat maxx = CGRectGetMaxX(self.messageView.bounds); 
CGFloat maxy = CGRectGetMaxY(self.messageView.bounds); 

UIBezierPath *path = [[UIBezierPath alloc] init]; 
[path moveToPoint:CGPointMake(minx + topLeftRadius, miny)]; 
[path addLineToPoint:CGPointMake(maxx - topRightRadius, miny)]; 
[path addArcWithCenter:CGPointMake(maxx - topRightRadius, miny + topRightRadius) radius: topRightRadius startAngle: 3 * M_PI_2 endAngle: 0 clockwise: YES]; 
[path addLineToPoint:CGPointMake(maxx, maxy - bottomRightRadius)]; 
[path addArcWithCenter:CGPointMake(maxx - bottomRightRadius, maxy - bottomRightRadius) radius: bottomRightRadius startAngle: 0 endAngle: M_PI_2 clockwise: YES]; 
[path addLineToPoint:CGPointMake(minx + bottomLeftRadius, maxy)]; 
[path addArcWithCenter:CGPointMake(minx + bottomLeftRadius, maxy - bottomLeftRadius) radius: bottomLeftRadius startAngle: M_PI_2 endAngle:M_PI clockwise: YES]; 
[path addLineToPoint:CGPointMake(minx, miny + topLeftRadius)]; 
[path addArcWithCenter:CGPointMake(minx + topLeftRadius, miny + topLeftRadius) radius: topLeftRadius startAngle: M_PI endAngle:3 * M_PI_2 clockwise: YES]; 
[path closePath]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.path = path.CGPath; 
self.messageView.layer.mask = maskLayer; 
+0

它是迅速嗎?,我是新的iOS,我不知道迅速。如果你不介意,你可以添加一個Objective-C版本。非常感謝你;) –

+0

是的,它是Swift。我已經將它翻譯成Obj-C。希望我沒有錯過任何';'或'[]' –

+0

非常感謝。但我現在必須回家,我明天會檢查它。那麼我會通知你;) –

0

這裏有一個UIBezierPath類別,你想要做什麼,下面的一些模式現有的初始化程序:

extension UIBezierPath { 
    public convenience init(roundedRect rect: CGRect, topLeftRadius: CGFloat?, topRightRadius: CGFloat?, bottomLeftRadius: CGFloat?, bottomRightRadius: CGFloat?) { 
     self.init() 

     assert(((bottomLeftRadius ?? 0) + (bottomRightRadius ?? 0)) <= rect.size.width) 
     assert(((topLeftRadius ?? 0) + (topRightRadius ?? 0)) <= rect.size.width) 
     assert(((topLeftRadius ?? 0) + (bottomLeftRadius ?? 0)) <= rect.size.height) 
     assert(((topRightRadius ?? 0) + (bottomRightRadius ?? 0)) <= rect.size.height) 

     // corner centers 
     let tl = CGPoint(rect.minX + (topLeftRadius ?? 0), rect.minY + (topLeftRadius ?? 0)) 
     let tr = CGPoint(rect.maxX - (topRightRadius ?? 0), rect.minY + (topRightRadius ?? 0)) 
     let bl = CGPoint(rect.minX + (bottomLeftRadius ?? 0), rect.maxY - (bottomLeftRadius ?? 0)) 
     let br = CGPoint(rect.maxX - (bottomRightRadius ?? 0), rect.maxY - (bottomRightRadius ?? 0)) 

     let topMidpoint = CGPoint(rect.midX, rect.minY) 

     makeClockwiseShape: do { 
      self.move(to: topMidpoint) 

      if let topRightRadius = topRightRadius { 
       self.addLine(to: CGPoint(rect.maxX - topRightRadius, rect.minY)) 
       self.addArc(withCenter: tr, radius: topRightRadius, startAngle: -CGFloat.pi/2, endAngle: 0, clockwise: true) 
      } 
      else { 
       self.addLine(to: tr) 
      } 

      if let bottomRightRadius = bottomRightRadius { 
       self.addLine(to: CGPoint(rect.maxX, rect.maxY - bottomRightRadius)) 
       self.addArc(withCenter: br, radius: bottomRightRadius, startAngle: 0, endAngle: CGFloat.pi/2, clockwise: true) 
      } 
      else { 
       self.addLine(to: br) 
      } 

      if let bottomLeftRadius = bottomLeftRadius { 
       self.addLine(to: CGPoint(rect.minX + bottomLeftRadius, rect.maxY)) 
       self.addArc(withCenter: bl, radius: bottomLeftRadius, startAngle: CGFloat.pi/2, endAngle: CGFloat.pi, clockwise: true) 
      } 
      else { 
       self.addLine(to: bl) 
      } 

      if let topLeftRadius = topLeftRadius { 
       self.addLine(to: CGPoint(rect.minX, rect.minY + topLeftRadius)) 
       self.addArc(withCenter: tl, radius: topLeftRadius, startAngle: CGFloat.pi, endAngle: -CGFloat.pi/2, clockwise: true) 
      } 
      else { 
       self.addLine(to: tl) 
      } 

      self.close() 
     } 
    } 
}