joe*_*oec 8 cocoa-touch uikit uiview
在我的一个方法中,我有这个代码:
-(void)myMethod {
UIBezierPath *circle = [UIBezierPath
bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)];
}
Run Code Online (Sandbox Code Playgroud)
如何让它在视图中显示?
我试过addSubview,但它给了我一个incompatible type error因为它期待一个UIView.
我相信这一定很简单.
谢谢
dav*_*Mac 27
我想补充一点,你不必在UIView的"drawRect:"方法中绘制它.您可以在UIGraphics图像上下文中的任何位置绘制它.当我不想创建UIView的子类时,我会一直这样做.这是一个有效的例子:
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];
Run Code Online (Sandbox Code Playgroud)
现在只需将UIImageView添加为子视图.
此外,您也可以将其用于其他绘图.再次,经过一些设置后,它就像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];
Run Code Online (Sandbox Code Playgroud)
编辑:我应该补充的一点是,对于任何这类现代绘画,你都应该换掉
UIGraphicsBeginImageContext(size);
Run Code Online (Sandbox Code Playgroud)
对于
UIGraphicsBeginImageContextWithOptions(size, opaque, scale);
Run Code Online (Sandbox Code Playgroud)
这将正确绘制视网膜和非视网膜显示器的图形.
仅供参考,对于可能具有一定透明度的视网膜显示器来说,这UIGraphicsBeginImageContext(size)相当于UIGraphicsBeginImageContextWithOptions(size, FALSE, 1.f)没问题.
但是,如果您不需要透明度,则更优化为opaque参数传递TRUE.
最安全和推荐的绘制方法是[[UIScreen mainScreen]scale]作为scale参数传入.
因此,对于上面的示例,您将使用此代替:
UIGraphicsBeginImageContextWithOptions(aSize, FALSE, [[UIScreen mainScreen] scale]);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看Apple的文档.
Vla*_*mir 18
您可以使用其中一个fill或多个stroke方法绘制它,例如在自定义视图的drawInRect:实现中:
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *circle = [UIBezierPath
bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)];
[circle fill];
}
Run Code Online (Sandbox Code Playgroud)
小智 10
您还可以使用CAShapeLayer将UIBezierPath添加到UIView而不进行子类化.
例如,要将路径添加为以UIView为中心的3pt白线:
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];
Run Code Online (Sandbox Code Playgroud)