2016-09-13 80 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

感謝。有效。我非常感謝你的幫助。 –