【发布时间】:2016-07-16 21:26:28
【问题描述】:
我正在尝试在触摸时绘制一个矩形。到目前为止,我有两段代码,第一段是当前绘制圆形的方法,第二段代码添加要查看的形状,我该如何绘制矩形?
func drawRectangle() {
// Get the Graphics Context
let context = UIGraphicsGetCurrentContext();
// Set the rectangle outerline-width
CGContextSetLineWidth(context, 5.0);
// Set the rectangle outerline-colour
UIColor.redColor().set()
// Create Rectangle
CGContextAddArc(context, (frame.size.width)/2, frame.size.height/2, (frame.size.width - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1)
// Draw
CGContextStrokePath(context);
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let rectangleCenter = touches.first!.locationInView(view)
let rectangleWidth = CGFloat(50)
let rectangleHeight = rectangleWidth
// Create a new rectangleView
let rectangleView = Annotations(frame: CGRectMake(rectangleCenter.x, rectangleCenter.y, rectangleWidth, rectangleHeight), shape: 1)
view.addSubview(rectangleView)
}
我尝试了以下方法:
// Create Rectangle
CGContextAddRect(context, CGRectMake((frame.size.width)/2, frame.size.height/2, 40, 40));
它创建矩形左上角的一部分,顶部有一条线,左下有一条线。如何将其调整为完整的矩形?
【问题讨论】:
-
如果您在 CGContext 参考中查找 CGContextAddArc ,那么 CGContextAddRect 就在不远处... :)
-
嗨 Martin R,我试过 CGContextAddRect,它给了我一半的矩形
-
那么你创建了错误的矩形。 CGRectMake 的前两个参数是原点(左上角),而不是中点。
-
Martin R 我可以看一个例子吗?
标签: swift core-graphics