-1
我试图画一个矩形触摸。到目前为止,我有两段代码,第一个是目前绘制一个圆的方法,第二个代码添加了形状以查看,我如何绘制矩形?Swift绘制矩形
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));
它创建与顶部的线与线下左边的矩形的左上角的一部分。我如何调整这是一个完整的矩形?
如果您在查找CGContextAddArc CGContext引用然后CGContextAddRect是不是很远...... :) –
嗨马丁R,我试过CGContextAddRect,它给了我一个矩形的一半 – user979331
然后你cre吃错了矩形。 CGRectMake的前两个参数是原点(左上角),而不是中点。 –