我建立一個iOS應用程序,我有一個名爲擴展UIView
「DrawableCanvas」級。這個想法是讓用戶能夠在故事板上繪製UIViews來擴展我創建的類。iOS的斯威夫特3 - 清除的UIView
的繪製工作正常,並順利在這一點上。下面是我使用的代碼:
class DrawableView: UIView {
let path=UIBezierPath()
var previousPoint:CGPoint
var lineWidth:CGFloat=1.0
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override init(frame: CGRect) {
previousPoint=CGPoint.zero
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
previousPoint=CGPoint.zero
super.init(coder: aDecoder)
let panGestureRecognizer=UIPanGestureRecognizer(target: self, action: #selector(pan))
panGestureRecognizer.maximumNumberOfTouches=1
self.addGestureRecognizer(panGestureRecognizer)
}
override func draw(_ rect: CGRect) {
// Drawing code
UIColor.black.setStroke()
path.stroke()
path.lineWidth=lineWidth
}
func pan(panGestureRecognizer:UIPanGestureRecognizer)->Void{
let currentPoint=panGestureRecognizer.location(in: self)
let midPoint=self.midPoint(p0: previousPoint, p1: currentPoint)
if panGestureRecognizer.state == .began{
path.move(to: currentPoint)
}
else if panGestureRecognizer.state == .changed{
path.addQuadCurve(to: midPoint,controlPoint: previousPoint)
}
previousPoint=currentPoint
self.setNeedsDisplay()
}
func midPoint(p0:CGPoint, p1:CGPoint)->CGPoint{
let x=(p0.x+p1.x)/2
let y=(p0.y+p1.y)/2
return CGPoint(x: x, y: y)
}
}
當然,我必須清除按鈕按下的觀點,這是我的按鈕按壓動作:
@IBAction func reset1(_ sender: AnyObject) {
print("reset hit!")
sig1View.setNeedsLayout()
sig1View.setNeedsDisplay()
}
其中sig1View
是我想要的看法重置。
這沒有工作了,所以我嘗試的卻是在DrawableView
類寫reloadData()
:
func reloadData() {
print("reload data hit")
self.setNeedsDisplay()
}
的功能得到了所謂的,但同樣的,沒有工作。
如何清除或iOS的斯威夫特3重置一個UIView?非常感謝領導和幫助。
你能做出'在view.layers.sublayers層{layer.removeFromSuperlayer()}'我沒有與吸引路徑很多經驗的電話,但是這就是我會先嚐試 –