xcode 如何用贝塞尔路径绘制矩形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15837800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw in rect with bezier path?
提问by Dima Deplov
I have a some rect in my view and I want to draw a figure in that rect with UIBezierPath.
我的视图中有一些矩形,我想用 UIBezierPath 在该矩形中绘制一个图形。
I thought that method
我以为那个方法
UIBezierPath myBezierPath = [UIBezierPath bezierPathWithRect:myRect];
will make an empty UIBezierPath called myBezierPath, with bounds that myRect gives to it, and then I can "draw" it that myBezierPath using moveToPoint and addLineToPoint. But, that code made a square (I'm stroke that path, so stroked square) with bounds that was in myRect. But I don't want him to be visible. I only want that if I try this:
将创建一个名为 myBezierPath 的空 UIBezierPath,带有 myRect 给它的边界,然后我可以使用 moveToPoint 和 addLineToPoint“绘制”它 myBezierPath。但是,该代码使用 myRect 中的边界制作了一个正方形(我正在绘制该路径,因此绘制了正方形)。但我不想让他被人看到。如果我尝试这个,我只想要:
NSLog(@"myBezierPath rect size %f,%f",myBezierPath.bounds.size.width,myBezierPath.bounds.size.height);
It returns me the width and height that myRect has. And therefore I can use it in my moveToPoint method and addLine method. For example like this:
它返回 myRect 的宽度和高度。因此我可以在我的 moveToPoint 方法和 addLine 方法中使用它。例如像这样:
[myBezierPath addLineToPoint: CGPointMake(suit.bounds.size.width/2, 0)];
Thanks.
谢谢。
My question is: ?How can I draw in rect using bezier path??
我的问题是:?如何使用贝塞尔路径绘制矩形?
I made this with method, here myRect have origin that place CGRect with myBezierPath in center of the view.
我用方法做了这个,这里 myRect 的原点是将 CGRect 和 myBezierPath 放在视图的中心。
myBezierPath = [UIBezierPath bezierPathWithRoundedRect:myRect cornerRadius:20.0];
Example 1 http://i.minus.com/iguFjeqZQrsb6.png
示例 1 http://i.minus.com/iguFjeqZQrsb6.png
How can I made similar (place my another drawing to center with origin)? Now I have this:
我如何制作类似的(将我的另一幅画以原点为中心)?现在我有这个:
回答by matt
Example (you will need a UIImageView in your interface):
示例(您的界面中将需要一个 UIImageView):
CGRect r = self.myImageView.bounds;
UIGraphicsBeginImageContextWithOptions(r.size, NO, 0);
UIBezierPath* p = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(r,10,10) cornerRadius:10];
[p stroke];
CGPoint c = CGPointMake(r.size.width/2.0, r.size.height/2.0);
p = [UIBezierPath bezierPath];
[p moveToPoint:CGPointMake(c.x-30, c.y)];
[p addLineToPoint:CGPointMake(c.x, c.y-30)];
[p addLineToPoint:CGPointMake(c.x+30, c.y)];
[p addLineToPoint:CGPointMake(c.x, c.y+30)];
[p closePath];
[p fill];
self.myImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();