繁体   English   中英

如何在不将手指从屏幕抬起的情况下结束长按手势?

[英]How to end long press gesture without lifting finger from screen?

我试图结束长按手势,而又不将手指从屏幕上抬起。 那有可能很快吗?

我正在制作一个可让您录制视频的应用程序。 当我按下按钮时,视频记录开始;当我从屏幕上抬起手指时,视频记录结束。 该部分工作完美。 我还想让长按手势在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()
    }
}

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM