2010-07-28 27 views
8

Trong một trong những phương pháp của tôi, tôi có mã này:Hiển thị UIBezierPath vào xem

-(void)myMethod { 
    UIBezierPath *circle = [UIBezierPath 
         bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)]; 
} 

Làm thế nào để tôi nhận được nó để hiển thị trên quan điểm?

Tôi đã thử addSubview nhưng nó đã cho tôi một incompatible type error vì mong đợi một UIView.

Tôi chắc chắn điều này phải đơn giản.

Cảm ơn

Trả lời

18

Bạn có thể vẽ nó bằng cách sử dụng fill hoặc stroke phương pháp ví dụ như trong giao diện tùy chỉnh của thi drawInRect::

- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    UIBezierPath *circle = [UIBezierPath 
        bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)]; 
    [circle fill]; 
} 
+0

Tôi cố gắng này, nhưng không có gì xảy ra. Làm thế nào để tôi có được bản vẽ xảy ra trong chế độ xem tùy chỉnh của tôi? – joec

+1

Tạo lớp con UIView và thực hiện phương thức drawRect ở đó, nó hoạt động với tôi ít nhất – Vladimir

+1

Đây là cũ, nhưng trong trường hợp nó giúp người khác: Tôi quên xác định rằng giao diện của tôi trong Interface Builder là loại "MyView" (nhận dạng lớp) – Mirkules

2

Vẽ là việc cung cấp độc quyền các quan điểm. Tạo chế độ xem tùy chỉnh, cung cấp cho nó đường dẫn của bạn và triển khai phương thức drawRect: của chế độ xem để điền và/hoặc vuốt đường dẫn.

+0

Vì vậy, tôi đã thực hiện một cái nhìn tùy chỉnh, sau đó đã làm điều này trong bộ điều khiển xem khác của tôi 'JogShuttle * js = [[JogShuttle alloc] initWithFrame: CGRectMake (...)]; [self.view addSubView: js]; 'nhưng không có gì được vẽ trên chế độ xem? – joec

+0

Bạn có thể đăng thêm một số mã không? Bạn đặt khung hình nào cho chế độ xem js của mình? Kiểm tra xem đường dẫn bezier của bạn có nằm trong giới hạn của khung nhìn không? – Vladimir

+0

đã giải quyết - cảm ơn. – joec

27

Chỉ cần nghĩ rằng tôi muốn thêm rằng bạn không nhất thiết phải vẽ điều này trong phương thức "drawRect:" của UIView. Bạn có thể vẽ nó bất cứ nơi nào bạn muốn cung cấp bạn làm điều đó bên trong một bối cảnh hình ảnh UIGraphics. Tôi làm điều này tất cả thời gian khi tôi không muốn tạo một lớp con của UIView. Dưới đây là ví dụ hoạt động:

UIBezierPath *circle = [UIBezierPath 
         bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)]; 

//you have to account for the x and y values of your UIBezierPath rect 
//add the x to the width (75 + 200) 
//add the y to the height (100 + 200) 
UIGraphicsBeginImageContext(CGSizeMake(275, 300)); 

//this gets the graphic context 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//you can stroke and/or fill 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor); 
[circle fill]; 
[circle stroke]; 

//now get the image from the context 
UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage]; 

Bây giờ, chỉ cần thêm UIImageView làm chế độ xem phụ.

Ngoài ra, bạn cũng có thể sử dụng tính năng này cho bản vẽ khác. Một lần nữa, sau một chút thiết lập, nó hoạt động giống như phương thức drawRect:

//this is an arbitrary size for example 
CGSize aSize = CGSizeMake(50.f, 50.f); 

//this can take any CGSize 
//it works like the frame.size would in the drawRect: method 
//in the way that it represents the context's size 
UIGraphicsBeginImageContext(aSize); 

//this gets the graphic context 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//you can do drawing just like you would in the drawRect: method 
//I am drawing a square just for an example to show you that you can do any sort of drawing in here 
CGContextMoveToPoint(context, 0.f, 0.f); 
CGContextAddLineToPoint(context, aSize.width, 0.f); 
CGContextAddLineToPoint(context, aSize.width, aSize.height); 
CGContextAddLineToPoint(context, 0.f, aSize.height); 
CGContextClosePath(context); 

//you can stroke and/or fill 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor); 
CGContextDrawPath(context, kCGPathFillStroke); 

//now get the image from the context 
UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage]; 

Edit: Một điều tôi nên thêm là đối với bất kỳ bản vẽ hiện đại ngày nay của loại hình này, bạn nên trao đổi ra

UIGraphicsBeginImageContext(size); 

cho

UIGraphicsBeginImageContextWithOptions(size, opaque, scale); 

này sẽ vẽ đồ họa của bạn một cách chính xác cho màn hình võng mạc và không võng mạc.

FYI, UIGraphicsBeginImageContext(size) tương đương với UIGraphicsBeginImageContextWithOptions(size, FALSE, 1.f), không có màn hình võng mạc nào có thể có độ trong suốt.

Tuy nhiên, nếu bạn không cần độ trong suốt, nó được tối ưu hóa hơn để chuyển TRUE cho đối số mờ.

Cách vẽ an toàn nhất và được đề xuất là để vượt qua trong [[UIScreen mainScreen]scale] làm đối số quy mô.

Vì vậy, cho ví dụ (s) ở trên, bạn sẽ sử dụng này để thay thế:

UIGraphicsBeginImageContextWithOptions(aSize, FALSE, [[UIScreen mainScreen] scale]); 

Mọi chi tiết, kiểm tra Apple's docs.

+0

Giải thích đơn giản, súc tích và hữu ích; +1. –

10

Bạn cũng có thể thêm UIBezierPath vào UIView mà không cần phân lớp bằng cách sử dụng CAShapeLayer.

Ví dụ, để thêm đường dẫn của bạn như một dòng trắng 3pt tập trung ở một UIView:

UIBezierPath *mybezierpath = [UIBezierPath 
        bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)]; 

CAShapeLayer *lines = [CAShapeLayer layer]; 
lines.path = mybezierpath.CGPath; 
lines.bounds = CGPathGetBoundingBox(lines.path); 
lines.strokeColor = [UIColor whiteColor].CGColor; 
lines.fillColor = [UIColor clearColor].CGColor; /*if you just want lines*/ 
lines.lineWidth = 3; 
lines.position = CGPointMake(self.myview.frame.size.width/2.0, self.myview.frame.size.height/2.0); 
lines.anchorPoint = CGPointMake(.5, .5); 

[self.myview.layer addSublayer:lines]; 
1

Trong Swift 2.0:

let path = UIBezierPath() 

let p1 = CGPointMake(0,self.view.frame.height/2) 
let p3 = CGPointMake(self.view.frame.width,self.view.frame.height/2) 

path.moveToPoint(p1) 
path.addQuadCurveToPoint(p3, controlPoint: CGPoint(x: self.view.frame.width/2, y: 0)) 

let line = CAShapeLayer() 
line.path = path.CGPath; 
line.strokeColor = UIColor.blackColor().CGColor 
line.fillColor = UIColor.redColor().CGColor 
view.layer.addSublayer(line) 
Các vấn đề liên quan