自定义 UIView 中的 UIPanGestureRecognizer

问题描述 投票:0回答:0

我想设置一个自定义的 UIView 类,其中包含

UIPanGestureRecognizer
s。但是,永远不会调用 pan 函数。我在“调试视图层次结构”中检查了
RangeSelector
视图在前面,确实如此。此外,我认为问题可能是我的
UINavigationController
正在拦截手势,所以我让父视图呈现模态。然而,仍然没有运气。谢谢。

import Foundation
import UIKit

class RangeSelector: UIView {

  private static var HANDLE_BAR_WIDTH: CGFloat = 10
  
  private var leftHandle: UIView = {
    let leftHandle = UIView()
    leftHandle.translatesAutoresizingMaskIntoConstraints = false
    leftHandle.backgroundColor = UIHelper.boldTint
    leftHandle.isUserInteractionEnabled = true
    return leftHandle
  }()
  
  private var rightHandle: UIView = {
    let rightHandle = UIView()
    rightHandle.translatesAutoresizingMaskIntoConstraints = false
    rightHandle.backgroundColor = UIHelper.boldTint
    rightHandle.isUserInteractionEnabled = true
    return rightHandle
  }()
  
  private var topBeam: UIView = {
    let beam = UIView()
    beam.translatesAutoresizingMaskIntoConstraints = false
    beam.backgroundColor = UIHelper.boldTint
    return beam
  }()
  
  private var bottomBeam: UIView = {
    let beam = UIView()
    beam.translatesAutoresizingMaskIntoConstraints = false
    beam.backgroundColor = UIHelper.boldTint
    return beam
  }()
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    self.isUserInteractionEnabled = true
    setupUI()
    setupUIConstraints()
    setupGestureRecognizers()
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) is not supported")
  }
  
  private func setupUI() {
    addSubview(topBeam)
    addSubview(bottomBeam)
    addSubview(leftHandle)
    addSubview(rightHandle)
  }

  private func setupUIConstraints() {
    NSLayoutConstraint.activate([
      leftHandle.leftAnchor.constraint(equalTo: leftAnchor, constant: -RangeSelector.HANDLE_BAR_WIDTH),
      leftHandle.heightAnchor.constraint(equalTo: heightAnchor),
      leftHandle.widthAnchor.constraint(equalToConstant: RangeSelector.HANDLE_BAR_WIDTH),
      leftHandle.centerYAnchor.constraint(equalTo: centerYAnchor)
    ])
    
    NSLayoutConstraint.activate([
      rightHandle.rightAnchor.constraint(equalTo: rightAnchor, constant: RangeSelector.HANDLE_BAR_WIDTH),
      rightHandle.heightAnchor.constraint(equalTo: heightAnchor),
      rightHandle.widthAnchor.constraint(equalToConstant: RangeSelector.HANDLE_BAR_WIDTH),
      rightHandle.centerYAnchor.constraint(equalTo: centerYAnchor)
    ])
    
    NSLayoutConstraint.activate([
      topBeam.leftAnchor.constraint(equalTo: leftHandle.rightAnchor),
      topBeam.rightAnchor.constraint(equalTo: rightHandle.leftAnchor),
      topBeam.topAnchor.constraint(equalTo: leftHandle.topAnchor),
      topBeam.heightAnchor.constraint(equalToConstant: 3)
    ])
    
    NSLayoutConstraint.activate([
      bottomBeam.leftAnchor.constraint(equalTo: leftHandle.rightAnchor),
      bottomBeam.rightAnchor.constraint(equalTo: rightHandle.leftAnchor),
      bottomBeam.bottomAnchor.constraint(equalTo: leftHandle.bottomAnchor),
      bottomBeam.heightAnchor.constraint(equalToConstant: 3)
    ])
  }
  
  private func setupGestureRecognizers() {
    let leftPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleLeftPanGesture(_:)))
    leftHandle.addGestureRecognizer(leftPanGestureRecognizer)
    
    let rightPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleRightPanGesture(_:)))
    rightHandle.addGestureRecognizer(rightPanGestureRecognizer)
  }

  @objc private func handleLeftPanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
    print("Moving left handle...")
    let translation = gestureRecognizer.translation(in: self)
    let newLeftHandlePosition = leftHandle.frame.origin.x + translation.x
    leftHandle.frame.origin.x = newLeftHandlePosition
    gestureRecognizer.setTranslation(CGPoint.zero, in: self)
  }
  
  @objc private func handleRightPanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
    print("Moving right handle...")
    let translation = gestureRecognizer.translation(in: self)
    let newRightHandlePosition = rightHandle.frame.origin.x + translation.x
    rightHandle.frame.origin.x = newRightHandlePosition
    gestureRecognizer.setTranslation(CGPoint.zero, in: self)
  }
}
swift uipangesturerecognizer
© www.soinside.com 2019 - 2024. All rights reserved.