Swift:Long Press手势识别器 - 检测水龙头和长按

时间:2015-01-21 01:20:43

标签: ios swift long-press

我想连接一个动作,如果手势是一个点击,它会以特定方式动画一个对象,但如果按下持续时间超过0.5秒,它会做其他事情。

现在,我只是把动画连接起来了。我不知道如何区分长按和水龙头? 如何访问印刷时间以实现上述目的?

 @IBAction func tapOrHold(sender: AnyObject) {
        UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {

            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {

                self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2))
            })
            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
                self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2))
            })
            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
                self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2))
            })

            }, completion: { (Bool) in
                let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView")
                self.showViewController(vc as UIViewController, sender: vc)
        })

4 个答案:

答案 0 :(得分:74)

定义两个IBActions并为每个Gesture Recognizer设置一个Gesture Recognizer。这样,您可以为每个手势执行两个不同的操作。

您可以在界面构建器中将每个@IBAction func tapped(sender: UITapGestureRecognizer) { println("tapped") //Your animation code. } @IBAction func longPressed(sender: UILongPressGestureRecognizer) { println("longpressed") //Different code } 设置为不同的IBAction。

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
    self.view.addGestureRecognizer(tapGestureRecognizer)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
    self.view.addGestureRecognizer(longPressRecognizer)

func tapped(sender: UITapGestureRecognizer)
{
    println("tapped")
}

func longPressed(sender: UILongPressGestureRecognizer)
{
    println("longpressed")
}

通过没有界面构建器的代码

{{1}}

答案 1 :(得分:6)

适用于swift2

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.featuredCouponColView.addGestureRecognizer(lpgr)

<强>动作

//MARK: - UILongPressGestureRecognizer Action -
    func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
        if gestureReconizer.state != UIGestureRecognizerState.Ended {
            //When lognpress is start or running
        }
        else {
            //When lognpress is finish
        }
    }

适用于Swift 4.2

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.colVw.addGestureRecognizer(lpgr)

//MARK: - UILongPressGestureRecognizer Action -
    @objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
        if gestureReconizer.state != UIGestureRecognizer.State.ended {
            //When lognpress is start or running
        }
        else {
            //When lognpress is finish
        }
    }

答案 2 :(得分:5)

通过没有界面构建器的代码

<?php 
$sContent = '';
if($_GET['f'] == 'showInfo') 
{
    $sContent .= 'This is the info about item id: '.$_GET['id'];
}
else
{
    $sContent .= '<script src="https://code.jquery.com/jquery-1.12.4.js"></script>'; 
    $sContent .= '<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>'; 
    $sContent .= '<script src="in.js" type="text/javascript"></script>';

    $id=2;
    $sContent .= '<div id="agree">Agree to terms: <input type="checkbox" id="agreed" value=1></div>';
    $sContent .= '<br><br><div class="show"></div>';

    $sAction = $_SERVER['PHP_SELF']."?id=".$id."&f=showInfo";
    $sDest .= '<br><input class="'.$id.'" type="button" id="'.$id.'" name="'.$id.'" value="Click to show info about item '.$id.'" onClick="javascript:showInfo(\''.$sAction.'\');">';

    $sContent .= $sDest;
}
echo $sContent;

但唯一的问题是长按手势运行两次。如果您找到任何解决方案,请在下面做出评论!

答案 3 :(得分:0)

Swift 5 使用界面构建器

对于普通点击,您只需从按钮创建 touch up inside 操作。

对于长按,为您的按钮创建一个插座,创建点击手势识别器并将其设置为按钮,然后创建选择器方法来执行长按任务。

@IBOutlet var myButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addToListButtonLongPress(_:)))
    longPressRecognizer.numberOfTouchesRequired = 1
    longPressRecognizer.allowableMovement = 10
    longPressRecognizer.minimumPressDuration = 0.5
    myButton.addGestureRecognizer(longPressRecognizer)
}

// Connected to myButton in interface builder.
@IBAction func myButtonTapped(_ sender: UIButton) {
    print("button tapped")
}

@objc func myButtonLongPressed(_ sender: UILongPressGestureRecognizer) {
    print("button long pressed")
}