写着玩儿的小程序,继续学习swift.运行效果+代码+知识点总结
运行效果:
代码:
Canvas类:画布,画图板状态管理、交互、处理手势
class Canvas:UIView{
//负责线条的生成、操作与管理
private let pathCreator:PathCreator
//是否处于擦除状态
private var isInErasering:Bool
//橡皮擦视图
private let eraserView:UIView
override init(frame: CGRect) {
isInErasering = false
pathCreator = PathCreator()
eraserView = UIView.init()
eraserView.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
eraserView.backgroundColor = UIColor.white
eraserView.alpha = 0
super.init(frame: frame)
self.backgroundColor = UIColor.black
self.addSubview(eraserView)
let revokeBut = UIButton(type: UIButtonType.system)
revokeBut.frame = CGRect(x: 20, y: 20, width: 80, height: 30)
revokeBut.setTitle("撤销", for: UIControlState.normal)
revokeBut.addTarget(self, action: #selector(revokeButClick), for: UIControlEvents.touchUpInside)
self.addSubview(revokeBut)
let cleanBut = UIButton(type: UIButtonType.system)
cleanBut.frame = CGRect(x: 110, y: 20, width: 80, height: 30)
cleanBut.setTitle("清空", for: UIControlState.normal)
cleanBut.addTarget(self, action: #selector(cleanButClick), for: UIControlEvents.touchUpInside)
self.addSubview(cleanBut)
let eraserBut = UIButton(type: UIButtonType.system)
eraserBut.frame = CGRect(x: 200, y: 20, width:80, height: 30)
eraserBut.setTitle("橡皮", for: UIControlState.normal)
eraserBut.setTitle("画笔", for: UIControlState.selected)
eraserBut.addTarget(self, action: #selector(eraserButClick(but:)), for: UICo