2016-05-10 50 views
0

我有一個按鈕,當按下將移動一個節點。如何在按下按鈕時重複這個不停的操作。我想我正在尋找touchesBegan和touchesEnded之間的東西。有點像touchesContinue,因爲我希望節點在按下按鈕時繼續移動。這是我迄今爲止所擁有的。有什麼會重複touchesBegan?

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

查找'NSTimer'。在touchesBegan:withEvent:中打開它,並在touchesEnded:withEvent:中將其關閉。 –

回答

0

使用NSTimer重複移動操作。在Touch Down上啓動timer,並讓計時器在Touch Up InsideTouch Up Outside上停止。

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

這是目標C

我建議你用觸及事件

創建定時更新對象的全球

TOUCH內部並拖動和EXIT使用這個我做到了

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

NSLog(@"SIngle TAPPED"); 

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

[self arrowUpperLipPosChanged:sender]; 

} 

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

TOUCH DOWN

- (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]; 
} 
相關問題