簡體   English   中英

在 UIScrollView 中禁用子視圖的滾動

[英]Disable scroll of a subview in a UIScrollView

我在滾動視圖的頂部有一個視頻播放器視圖,我添加了垂直滑動來調整音量和亮度功能。

問題是當我在播放器視圖中滑動以調整音量時,滾動視圖也會滾動,我該如何防止呢?

這可能適用於您的用例。

我假設您在視頻視圖上添加了一個pan gesture ,因此在您的操作處理程序中,您可以在手勢的 state 為.began.changed時禁用滾動視圖滾動,並為任何 state 重新啟用它,例如endedcancelled

這是我的做法:

@objc
func didPan(_ panGesture: UIGestureRecognizer)
{
    scrollView.isScrollEnabled
        = panGesture.state != .began && panGesture.state != .changed
    
    /* The above is the same as these lines of code
    if panGesture.state == .began || panGesture.state == .ended
    {
        scrollView.isScrollEnabled = false
        return
    }
    
    scrollView.isScrollEnabled = true
    */
}

以下是此代碼所發生情況的示例:

  1. 當我平移視頻時,它顯示 label 並停止滾動視圖移動
  2. 當我在滾動視圖內但在視頻區域(黃色區域)之外的另一個視圖上平移時,它處於活動狀態

UIView ios Swift 上 UIScrollView 中的 AVPlayer 平移手勢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM