Swift 调用draw()函数时使用什么参数?
我想在swift中以编程方式画一条线。我想我已经有了它的工作代码,但是调用这个函数需要一个Swift 调用draw()函数时使用什么参数?,swift,line,draw,Swift,Line,Draw,我想在swift中以编程方式画一条线。我想我已经有了它的工作代码,但是调用这个函数需要一个CGRect参数。我不确定在那里写什么代码 draw()类和函数如下所示: class LineView : UIView { override func draw(_ rect: CGRect) { var aPath = UIBezierPath() aPath.move(to: CGPoint(x:2, y:2)) aPath.addLine
CGRect
参数。我不确定在那里写什么代码
draw()类和函数如下所示:
class LineView : UIView {
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
}
}
var line = LineView()
line.draw(MISSING ARGUMENT)
class ViewController: UIViewController {
@IBOutlet weak var gameBoard: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var line = LineView()
gameBoard.addSubview(line)
}
}
import UIKit
class LineView : UIView {
override init(frame: CGRect) {
super.init(frame: frame) //super = DO REGULAR INIT STUFF FOR UIView
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
UIColor.red.set()
aPath.stroke()
aPath.fill()
setNeedsDisplay()
}
}
现在,从主ViewDidLoad
调用它,看起来如下所示:
class LineView : UIView {
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
}
}
var line = LineView()
line.draw(MISSING ARGUMENT)
class ViewController: UIViewController {
@IBOutlet weak var gameBoard: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var line = LineView()
gameBoard.addSubview(line)
}
}
import UIKit
class LineView : UIView {
override init(frame: CGRect) {
super.init(frame: frame) //super = DO REGULAR INIT STUFF FOR UIView
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
UIColor.red.set()
aPath.stroke()
aPath.fill()
setNeedsDisplay()
}
}
但我不知道我应该通过什么样的论证。函数中没有使用任何CGRect
,因此我甚至不确定它的用途
更新
大体上,我创建的对象如下所示:
class LineView : UIView {
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
}
}
var line = LineView()
line.draw(MISSING ARGUMENT)
class ViewController: UIViewController {
@IBOutlet weak var gameBoard: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var line = LineView()
gameBoard.addSubview(line)
}
}
import UIKit
class LineView : UIView {
override init(frame: CGRect) {
super.init(frame: frame) //super = DO REGULAR INIT STUFF FOR UIView
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
UIColor.red.set()
aPath.stroke()
aPath.fill()
setNeedsDisplay()
}
}
我的绘画课是这样的:
class LineView : UIView {
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
}
}
var line = LineView()
line.draw(MISSING ARGUMENT)
class ViewController: UIViewController {
@IBOutlet weak var gameBoard: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var line = LineView()
gameBoard.addSubview(line)
}
}
import UIKit
class LineView : UIView {
override init(frame: CGRect) {
super.init(frame: frame) //super = DO REGULAR INIT STUFF FOR UIView
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
var aPath = UIBezierPath()
aPath.move(to: CGPoint(x:2, y:2))
aPath.addLine(to: CGPoint(x:6, y:6))
aPath.close()
UIColor.red.set()
aPath.stroke()
aPath.fill()
setNeedsDisplay()
}
}
让它与:
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
不要自己调用draw(rect:
。永远不要这样做
在视图初始化之后,框架隐式地调用它一次
如果要重画视图,请在视图上调用setNeedsDisplay()
。不要自己调用draw(rect:
。永远不要这样做
在视图初始化之后,框架隐式地调用它一次
如果您想在视图上重新绘制视图调用
setNeedsDisplay()
。我只是尝试了var line=LineView()游戏板。addSubview(line)
,但没有显示。我应该有一个构造函数吗?通常您必须初始化调用init的视图(frame
并至少传递宽度和高度,或应用适当的自动布局约束。我添加了一个构造函数并刚刚启动了对象,请参阅我的更新。但仍然没有显示任何内容…添加的初始值设定项是无意义的,因为它们只调用super
。您必须调用init(frame
并指定一个rect,例如let line=LineView(frame:CGRect(x:0,y:0,width:64,height:64))
Ah,现在使用init
就可以了。感谢您的帮助@vadian!!:)我刚刚试过var line=LineView()游戏板。addSubview(line)
但是没有显示任何内容。我应该有一个构造函数吗?通常你必须初始化一个调用init的视图(frame
并至少传递宽度和高度,或应用适当的自动布局约束。我添加了一个构造函数并刚刚启动了对象,请参阅我的更新。但仍然没有显示任何内容…添加的初始值设定项是无意义的,因为它们只调用super
。您必须调用init(frame
并指定一个rect,例如let line=LineView(frame:CGRect(x:0,y:0,width:64,height:64))
Ah,现在使用init
就可以了。感谢您的帮助@vadian!!:)