我正在尝试创建一个绘图应用程序,用户只需轻轻一按即可绘制。如果用户移除了触摸,则他/她再也不能通过触摸来进行绘制。因此,只需触摸第一次并轻扫,用户就需要绘制。
在我使用的代码中,用户仍然能够按照他/她想要的次数触摸和绘制。我希望用户只能在第一次触摸时绘制。
override func touchesBegan(touches: Set<UITouch>,
withEvent event: UIEvent?) {
swiped = false
if let touch = touches.first {
lastPoint = touch.locationInView(self.imageView)
}
}
override func touchesMoved(touches: Set<UITouch>,
withEvent event: UIEvent?){
swiped = true;
if let touch = touches.first {
let currentPoint = touch.locationInView(imageView)
UIGraphicsBeginImageContext(self.imageView.frame.size)
self.imageView.image?.drawInRect(CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height))
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y)
CGContextSetLineCap(UIGraphicsGetCurrentContext(),CGLineCap.Round)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0)
CGContextStrokePath(UIGraphicsGetCurrentContext())
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
lastPoint = currentPoint
}
}
override func touchesEnded(touches: Set<UITouch>,
withEvent event: UIEvent?) {
if(!swiped) {
// This is a single touch, draw a point
UIGraphicsBeginImageContext(self.imageView.frame.size)
self.imageView.image?.drawInRect(CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height))
CGContextSetLineCap(UIGraphicsGetCurrentContext(), CGLineCap.Round)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0)
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y)
CGContextStrokePath(UIGraphicsGetCurrentContext())
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
答案 0 :(得分:0)
使用标志允许触摸
var allowTouches = true
func touchesBegan() {
guard allowTouches else {
Return
}
// Your logic
}
func touchesMoved() {
guard allowTouches else {
Return
}
// Your logic
}
func touchesEnded() {
allowTouches = false
}