【问题标题】:How to end long press gesture without lifting finger from screen?如何在不从屏幕上抬起手指的情况下结束长按手势?
【发布时间】:2017-12-29 09:56:58
【问题描述】:

我正在尝试在不将手指从屏幕上抬起的情况下结束长按手势。这可能很快吗?

我正在制作一款可让您录制视频的应用。视频录制在我按下按钮时开始,当我将手指从屏幕上移开时结束。那部分工作得很好。如果我的手指仍然按下按钮,我还想要长按手势在 30 秒后结束。我实际上让它停止录制,但问题是录制停止时长按手势实际上并没有结束。

这是我的一些代码:

func stop() {
    let seconds : Int64 = 5
    let preferredTimeScale : Int32 = 1
    let maxDuration : CMTime = CMTimeMake(seconds, preferredTimeScale)
    movieOutput.maxRecordedDuration = maxDuration

    if movieOutput.recordedDuration == movieOutput.maxRecordedDuration {
       movieOutput.stopRecording()
    }
}

func longTap(_ sender: UILongPressGestureRecognizer){
    print("Long tap")

    stop()

    if sender.state == .ended {
        print("end end")
        movieOutput.stopRecording()
    }
    else if sender.state == .began {
        print("begin")
        captureSession.startRunning()
        startRecording()
    }
}

【问题讨论】:

  • 如何结束?你的问题不清楚。你试过什么?你有什么问题?
  • 对不起。我正在制作一个可以让您录制视频的应用程序。视频录制在我按下按钮时开始,当我将手指从屏幕上移开时结束。那部分工作得很好。如果我的手指仍然按下按钮,我还想要长按手势在 30 秒后结束。我实际上让它停止录制,但问题是录制停止时长按手势实际上并没有结束。
  • 所有这些细节都属于您的问题,而不是评论。
  • 为什么在手势处理程序中无条件调用stop?处理程序被多次调用。
  • 我只是想看看它是否有效。我把它放在 viewDidLoad 中,但它没有用。我完全迷路了,所以我只是在尝试随机的东西。

标签: ios swift uilongpressgesturerecogni


【解决方案1】:

您可以尝试使用计时器来取消手势:

class myController:UIViewController {
var timer:Timer! = nil
var lpr:UILongPressGestureRecognizer! = nil

override func viewDidLoad() {
    super.viewDidLoad()

    lpr = UILongPressGestureRecognizer()
    lpr.minimumPressDuration = 0.5
    lpr.numberOfTouchesRequired = 1
    // any other gesture setup
    lpr.addTarget(self, action: #selector(doTouchActions))
    self.view.addGestureRecognizer(lpr)


}

func createTimer() {
    if timer == nil {
    timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(cancelTrouchFromTimer), userInfo: nil, repeats: false)
    }
}

func cancelTimer() {
    if timer != nil {
    timer.invalidate()
    timer = nil
    }
}

func cancelTrouchFromTimer() {
    lpr.isEnabled = false
    //do all the things
    lpr.isEnabled = true

}

func doTouchActions(_ sender: UILongPressGestureRecognizer) {
    if sender.state == .began {
        createTimer()
    }

    if sender.state == .ended {// same for other states .failed .cancelled {
    cancelTimer()
    }

}

// cancel timer for all cases where the view could go away, like in deInit
 func deinit {
    cancelTimer()
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多