我正在嘗試學習swift bu,看來我已經卡住了。 有人可以指出爲什麼{ didSet { setNeedsDisplay() } }
似乎不工作?! 我們的目標是在用戶點擊 這裏是我的控制器,以畫小圈:Swift不會調用繪製(GRect)不止一次
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UIView!{
didSet{
display.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(addCirclePoint(byReactingTo:))))
}
}
let pointsAndLines = PointsAndLinesView()
func addCirclePoint(byReactingTo tapRecognizer: UITapGestureRecognizer){
let point = tapRecognizer.location(in: display)
pointsAndLines.point = point
}
}
這裏是我的看法:
import UIKit
class PointsAndLinesView: UIView {
var point = CGPoint(){ didSet{ setNeedsDisplay() } }
private func addCirclePoint()->UIBezierPath{
let circlePoint = UIBezierPath(arcCenter: point, radius: 5.0, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
circlePoint.lineWidth = 4
print("Did I run?!")
return circlePoint
}
override func draw(_ rect: CGRect) {
UIColor.black.set()
addCirclePoint().stroke()
}
}
期待您的答覆。
編輯:
對於萬一有人備查需要它的錯誤是,
的顯示是PointsAndLinesView的子類,不是UIView的
的和display.point應設置。
我可能是錯的,但'setNeedsDisplay'沒有'bool'參數? – Arc676
沒有布爾需要。如果爲空,則整個視圖失效,但您可以傳遞CGRect以使視圖區域無效。 –