有什么东西可以重复触摸开始吗?

时间:2016-05-10 01:11:29

标签: ios swift uibutton

我有一个按钮,按下按钮会移动一个节点。如何在按下按钮时重复此不停止。我想我正在寻找touchesBegan和touchesEnded之间的东西。像touchesContinue之类的东西,因为我希望节点在按下按钮时继续移动。这是我到目前为止所做的。

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)
        }
    }

2 个答案:

答案 0 :(得分:0)

这是目标C

我建议你使用这个我用触地事件做的

创建Timer对象全局

触摸内部并拖动并退出

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

NSLog(@"SIngle TAPPED");

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

[self arrowUpperLipPosChanged:sender];

}

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

接触

- (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];
}

答案 1 :(得分:0)

使用NSTimer重复移动操作。在timer上启用Touch Down,并将计时器停在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)
        }
    }

}