2015-06-19 13 views
7

Tôi có một vấn đề cắt một cái nhìn sử dụng CAShapeLayer-UIBezierPath, tôi muốn cắt nội dung nhưng tôi cuối cùng nhận được một cơn đột quỵ (frame) với UIBezierPath rằng, Đây là mã của tôiclip-che UIView với CAShapeLayer và UIBezierPath

UIBezierPath *path2Path = [UIBezierPath bezierPath]; 
[path2Path moveToPoint:CGPointMake(206.745, 0)]; 
[path2Path addLineToPoint:CGPointMake(206.745, 97.613)]; 
[path2Path addLineToPoint:CGPointMake(0, 97.613)]; 
[path2Path addLineToPoint:CGPointMake(0, 0)]; 
[path2Path addLineToPoint:CGPointMake(87.28, 0)]; 
[path2Path addCurveToPoint:CGPointMake(103.808, 12.118) controlPoint1:CGPointMake(87.28, 0) controlPoint2:CGPointMake(86.555, 12.118)]; 
[path2Path addCurveToPoint:CGPointMake(119.466, 0) controlPoint1:CGPointMake(121.061, 12.118) controlPoint2:CGPointMake(119.466, 0)]; 
[path2Path addLineToPoint:CGPointMake(206.745, 0)]; 
[path2Path closePath]; 

[path2Path addClip]; 

CAShapeLayer *pathLayer = [CAShapeLayer layer]; 
pathLayer.frame=MYVIEW.bounds; 
pathLayer.path = path2Path.CGPath; 

pathLayer.strokeColor = [[UIColor blackColor] CGColor]; 
pathLayer.fillColor = [[UIColor clearColor] CGColor]; 

pathLayer.fillRule=kCAFillRuleEvenOdd; 
[MYVIEW.layer setMask:pathLayer]; 
[MYVIEW.layer setMasksToBounds:YES]; 

MYVIEW.backgroundColor=[UIColor greenColor]; 

kết quả của đoạn mã này chỉ là một dòng đột quỵ xanh, bờ cõi rỗng, như thế này http://i.stack.imgur.com/aehdo.png

Tuy nhiên, tôi muốn làm cho giới hạn màu xanh lá cây, cắt bớt bởi đột quỵ mà

+0

Bạn muốn hiển thị phần duy nhất của hình ảnh bên trong đường bezier. Đúng? – Rajesh

+0

có chính xác. @RajeshMaurya – Aproram

+0

Tôi có thể biết không? nơi bạn viết mã? Trong Viewcontroller hoặc tạo ra lớp con UIView. – Rajesh

Trả lời

13

rob mayoff cho biết Bạn có thể thực hiện việc này dễ dàng bằng cách đặt mặt nạ lớp của chế độ xem của bạn thành một CAShapeLayer.

UIBezierPath *myClippingPath = ... 
CAShapeLayer *mask   = [CAShapeLayer layer]; 
mask.path     = myClippingPath.CGPath; 
myView.layer.mask   = mask; 

Trong Swift

let myClippingPath = UIBezierPath(...) 
let mask   = CAShapeLayer() 
mask.path   = myClippingPath.CGPath 
layer.mask   = mask 
2

Cám ơn câu trả lời chàng trai.

Trong trường hợp ai đó không thể tìm thấy câu trả lời phù hợp trên SO cho câu hỏi này trong nhiều giờ, như tôi vừa làm, tôi đã tập hợp một ý chính làm việc tại Swift 2.2 cho mặt nạ/cắt UIView với CGRect/UIBezierPath:

https://gist.github.com/Flar49/7e977e81f1d2827f5fcd5c6c6a3c3d94

extension UIView { 
    func mask(withRect rect: CGRect, inverse: Bool = false) { 
     let path = UIBezierPath(rect: rect) 
     let maskLayer = CAShapeLayer() 

     if inverse { 
      path.appendPath(UIBezierPath(rect: self.bounds)) 
      maskLayer.fillRule = kCAFillRuleEvenOdd 
     } 

     maskLayer.path = path.CGPath 

     self.layer.mask = maskLayer 
    } 

    func mask(withPath path: UIBezierPath, inverse: Bool = false) { 
     let path = path 
     let maskLayer = CAShapeLayer() 

     if inverse { 
      path.appendPath(UIBezierPath(rect: self.bounds)) 
      maskLayer.fillRule = kCAFillRuleEvenOdd 
     } 

     maskLayer.path = path.CGPath 

     self.layer.mask = maskLayer 
    } 
} 

Cách sử dụng:

let viewSize = targetView.bounds.size 
let rect = CGRect(x: 20, y: 20, width: viewSize.width - 20*2, height: viewSize.height - 20*2) 

// Cuts rectangle inside view, leaving 20pt borders around 
targetView.mask(withRect: rect, inverse: true) 

// Cuts 20pt borders around the view, keeping part inside rect intact 
targetView.mask(withRect: rect) 

Hy vọng nó sẽ tiết kiệm được một người nào đó một thời gian trong tương lai :)

Các vấn đề liên quan