2016-05-10 15 views
0

Ich habe einen Knopf, der beim Drücken einen Knoten bewegt. Wie kann ich diesen Non-Stop-Vorgang wiederholen, während die Taste gedrückt wird? Ich denke, ich suche etwas zwischen BerührungenBegan und berührtEnded. Etwas wie touchesContinue, weil ich möchte, dass der Knoten sich weiter bewegt, während die Taste gedrückt wird. Das habe ich bisher.Gibt es etwas, das Berührungen wiederholen wirdBegan?

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    // 1 
    leftMove.name = "Left" 
    rightMove.name = "Right" 

    /* Called when a touch begins */ 
    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 
     let node = self.nodeAtPoint(location) 

     if (node.name == "Left") { 
      // Implement your logic for left button touch here: 
      player.position == CGPoint(x:player.position.x-1, y:player.position.y) 
     } else if (node.name == "Right") { 
      // Implement your logic for right button touch here: 
      player.position = CGPoint(x:player.position.x+1, y:player.position.y) 
     } 
    } 
+0

Suchen Sie nach 'NSTimer'. Schalten Sie es in 'touchesBegan: withEvent:' ein und schalten Sie es in 'touchesEnded: withEvent:' aus. –

Antwort

0

Verwenden Sie NSTimer, um die Bewegungsaktion zu wiederholen. Lassen Sie die timer über die Touch Down gestartet werden und lassen Sie den Timer entweder unter Touch Up Inside oder Touch Up Outside stoppen.

import UIKit 

class ViewController: UIViewController, UIScrollViewDelegate { 

    var button: UIButton! 

    var timer: NSTimer! 

    leftMove.name = "Left" 
    rightMove.name = "Right" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     button = UIButton(frame: CGRectMake(100, 100, 50, 50)) as UIButton 

     //add Target for .TouchDown events, when user touch down 
     button.addTarget(self, action: #selector(ViewController.touchDown(_:)), forControlEvents: .TouchDown) 
     //add Target for .TouchUpInside events, when user release the touch 
     button.addTarget(self, action: #selector(ViewController.release(_:)), forControlEvents: .TouchUpInside) 

     view.addSubview(button) 

    } 

    func touchDown(sender: UIButton) { 
     //start the timer 
     timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: #selector(ViewController.movePlayer(_:)), userInfo: nil, repeats: true) 
    } 

    func release(sender: UIButton) { 
     //stop the timer 
     self.timer.invalidate() 
     self.timer = nil 

    } 

    func movePlayer(sender: NSTimer) { 
     //move your stuff here 
     let location = touch.locationInNode(self) 
     let node = self.nodeAtPoint(location) 

     if (node.name == "Left") { 
      // Implement your logic for left button touch here: 
      player.position == CGPoint(x:player.position.x-1, y:player.position.y) 
     } else if (node.name == "Right") { 
      // Implement your logic for right button touch here: 
      player.position = CGPoint(x:player.position.x+1, y:player.position.y) 
     } 
    } 

} 
0

THIS in Objective C IS

Ich schlage vor, Sie dies nutzen i mit Touch-did it Down-Ereignis

erstellen Timer globales Objekt

TOUCH UP INSIDE und Drag and EXIT

- (IBAction)arrowUpUpperLipTapped:(UIButton *)sender { 

NSLog(@"SIngle TAPPED"); 

if (self.timer1) { 
    [self.timer1 invalidate]; 
    self.timer1 = nil; 
} 

[self arrowUpperLipPosChanged:sender]; 

} 

// ------- -------------------------------------------------- --------------

Aufsetzpunkt

- (IBAction)arrowUpperLipLongTapped:(UIButton *)sender { 
    NSLog(@"LONG TAPPED"); 
if (self.timer) { 
    [self.timer1 invalidate]; 
    self.timer1 = nil; 
} 
_timer1 = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(arrowUpperLipTimerAction:) userInfo:sender repeats:YES]; 
} 

// ------------------- -------------------------------------------------- -

- (void) arrowUpperLipPosChanged :(UIButton *) sender { 

CGRect frame = self.viewUpperLip.frame; 
if (sender.tag == 9001) { 
    // UP Arrow Tapped 
    frame.origin.y --; 
} else if (sender.tag == 9002) { 
    frame.origin.y ++; 
} 
if (!CGRectContainsPoint(CGRectMake(frame.origin.x, frame.origin.y + frame.size.height/2, frame.size.width, frame.size.height), self.imgViewGridHEyesCenter.center) && !(CGRectIntersection(frame, [self.view viewWithTag:203].frame).size.height >= frame.size.height)) { 
    [self.viewUpperLip setFrame:frame]; 
} 

} 

// --------------------------------------- --------------------------------

- (void) arrowUpperLipTimerAction :(NSTimer *) timer { 
UIButton *sender = (UIButton *)[timer userInfo]; 
[self arrowUpperLipPosChanged:sender]; 
} 
Verwandte Themen