Swift 快速绘制矩形
我试着在触摸屏上画一个矩形。到目前为止,我有两段代码,第一段是当前绘制圆的方法,第二段代码将形状添加到视图中,如何绘制矩形Swift 快速绘制矩形,swift,core-graphics,Swift,Core Graphics,我试着在触摸屏上画一个矩形。到目前为止,我有两段代码,第一段是当前绘制圆的方法,第二段代码将形状添加到视图中,如何绘制矩形 func drawRectangle() { // Get the Graphics Context let context = UIGraphicsGetCurrentContext(); // Set the rectangle outerline-width CGContextSetLineWidth(context, 5.0);
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));
增加此处的宽度和高度:
let rectangleWidth = CGFloat(100)
let rectangleHeight = rectangleWidth
还有我的矩形方法
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
CGContextAddRect(context, CGRectMake(0, 0, 100, 100));
// Draw
CGContextStrokePath(context);
}
对于swift 4.2:
此代码用于swift 4.2
func drawRectangle()
{
// Get the Graphics Context
let context = UIGraphicsGetCurrentContext()
// Set the rectangle outerline-width
context?.setLineWidth( 5.0)
// Set the rectangle outerline-colour
UIColor.red.set()
// Create Rectangle
context?.addRect( CGRect(x: 0, y: 0, width: 100, height: 100))
// Draw
context?.strokePath()
}
我可以通过以下方式解决我的问题:
// Create Rectangle
CGContextAddRect(context, CGRectMake((frame.size.width)/2, frame.size.height/2, 40, 40));
增加此处的宽度和高度:
let rectangleWidth = CGFloat(100)
let rectangleHeight = rectangleWidth
还有我的矩形方法
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
CGContextAddRect(context, CGRectMake(0, 0, 100, 100));
// Draw
CGContextStrokePath(context);
}
对于swift 4.2:
此代码用于swift 4.2
func drawRectangle()
{
// Get the Graphics Context
let context = UIGraphicsGetCurrentContext()
// Set the rectangle outerline-width
context?.setLineWidth( 5.0)
// Set the rectangle outerline-colour
UIColor.red.set()
// Create Rectangle
context?.addRect( CGRect(x: 0, y: 0, width: 100, height: 100))
// Draw
context?.strokePath()
}
如果在CGContext引用中查找CGContextAddArc,则CGContextAddRect就在不远处…:)嗨,Martin R,我试过CGContextAddRect,它给了我半个矩形,然后你创建了一个错误的矩形。CGRectMake的前两个参数是原点(左上角),而不是中点。Martin R我能看一个例子吗?如果你在CGContext引用中查找CGContextAddArc,那么CGContextAddRect就不远了…:)嗨,Martin R,我试过CGContextAddRect,它给了我半个矩形,然后你创建了一个错误的矩形。CGRectMake的前两个参数是原点(左上角),而不是中点。Martin R我可以看一个例子吗?只是一个注释,我在
drawRectangle()中调用drawRectangle()
时才能看到这个矩形。UIView
的方法,请参见。只是一个注释,我在调用drawRectangle()之前无法看到这个矩形
在绘图(:)
方法的ui视图中,请参阅。