【问题标题】:TouchDown Event UIbutton to record Audio not triggered, due to Collectionview由于Collectionview,TouchDown Event UIbutton记录音频未触发
【发布时间】:2016-11-15 11:10:28
【问题描述】:

我正在使用JSQMessagesViewController 来实现聊天。我添加了一个录制按钮,并希望通过按住该按钮来发送音频消息以录制并通过释放按钮来停止录制。 目前我正在尝试这样做:

record_button = UIButton(type: UIButtonType.custom)
    record_button.tintColor = UIColor.white
    record_button.setImage(recordImage, for: .normal)

    self.recordingSession = AVAudioSession.sharedInstance()

    do {
        try self.recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try self.recordingSession.setActive(true)
        self.recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    self.record_button.addTarget(self, action: #selector(ChatViewController.recording), for: .touchDown )
                    self.record_button.addTarget(self, action: #selector(ChatViewController.recordTapRelease), for: .touchUpInside)
                    self.record_button.addTarget(self, action: #selector(ChatViewController.recordTapRelease), for: .touchDragExit)
                } else {
                    // failed to record!
                }
            }
        }
    } catch {
        // failed to record!
    }

不幸的是,它无法正常工作。它仅在我按下按钮向上移动时起作用。只有这样它才能识别保持事件并记录。当我触摸并按住同一位置时,它不会记录,当我释放触摸时,它会发送 1 秒的音频消息。

编辑 我意识到问题在于我的 Collectionview 有一个 ScrollView。 scrollview 会延迟 touchdown 事件以检查用户是否滚动。无论如何我应该如何触发触地事件?

【问题讨论】:

    标签: ios swift audio uibutton audio-recording


    【解决方案1】:

    好吧,您可以改用手势识别器。

    let gesture = UILongPressGestureRecognizer(target: self, action: #selector(longTouched))
    button.addGestureRecognizer(gesture)
    
    func longTouched(gesture: UIGestureRecognizer) {
        if gesture.state == .began {
          //triggered after long pressed
        }else if gesture.state == .ended || gesture.state == .cancelled {
          //end of the touch
        }
    }
    

    如果您需要检测触摸的开始,您还可以在按钮上添加一个 UITapGestureRecognizer。 在这种情况下,您可能希望:

    tapGesture.numberOfTapsRequired = 1
    

    【讨论】:

    • 之前试过这个,没有成功。
    • 好吧,我确实在我的项目中这样做了,它可以工作 :) 虽然无法打开完整的源代码。
    • 对我来说问题是滚动视图控制器。检查我的编辑。
    • 如果您的底部栏不在 collectionView 项目列表中,它应该可以工作。我会为底部栏创建一个 UIView 并将其添加到 Controller.view
    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多