2016-09-13 79 views
1

我正在尝试制作一个绘图应用程序,用户只需单击一下即可绘制应用程序。如果用户移开触摸,他/她将不能通过再次触摸来再次触摸。因此,通过简单地触摸第一次并轻扫,用户需要画画。防止第二次触摸生效

在我使用的代码中,用户仍然能够触摸和绘制尽可能多的次数,因为他/她想要。我希望用户只能在第一次触摸时画画。

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

使用标志,让触摸

var allowTouches = true 

func touchesBegan() { 
    guard allowTouches else { 
     Return 
    } 
    // Your logic 
} 

func touchesMoved() { 
    guard allowTouches else { 
     Return 
    } 
    // Your logic 
} 

func touchesEnded() { 
    allowTouches = false 
} 
+0

感谢。有效。我非常感谢你的帮助。 –