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

USEフラグは、タッチ

var allowTouches = true 

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

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

func touchesEnded() { 
    allowTouches = false 
} 
+0

感謝を可能にします。出来た。私は本当にあなたの助けに感謝します。 –

関連する問題