微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在另一个 ViewController Swift 中处理手势?

如何解决如何在另一个 ViewController Swift 中处理手势?

我有一个 customSwitch UIView 类,在该类中,我对其应用了一些点击和平移手势。现在,我将该类作为一个组件,就像我可以在任何地方重用它一样,但问题是如果我在其他任何地方声明该自定义开关类,我将无法获得该自定义开关的平移和点击手势。我的问题是如何访问这些点击和平移手势?谢谢。

CustomSwitch.Swift

class CustomSwitch : UIView {

var minMaxButtonConstraint:CGFloat = 0
var buttonLeadingConstraint:NSLayoutConstraint!
var isSwitched = false
var tapGest = UITapGestureRecognizer()
var panGest = UIPanGestureRecognizer()


var button:UIButton = {
   
    let btn = UIButton()
    btn.translatesAutoresizingMaskIntoConstraints = false
    btn.layer.cornerRadius = 6
    btn.backgroundColor = .white
    btn.imageView?.layer.transform = CATransform3DMakeScale(1.5,1.5,1.5)
    btn.addTarget(self,action: #selector(switchDirection),for: .touchUpInside)
    return btn
    
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.layer.cornerRadius = 10
    self.backgroundColor = .switchGrey
    setupViews()
}

func setupViews() {
    addSubview(button)
    declareGestureRecognizers()
    
    NSLayoutConstraint.activate([
        
        button.topAnchor.constraint(equalTo: self.topAnchor,constant: 4),button.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: -4),button.widthAnchor.constraint(equalToConstant: 12),])
    
        // Taken these constraints separately so that a reference can be maintained to these constraints and can be changed when needed
        buttonLeadingConstraint = button.leadingAnchor.constraint(equalTo: self.leadingAnchor,constant: 4)
        buttonLeadingConstraint.isActive = true
}

func declareGestureRecognizers() {
    
    
    tapGest = UITapGestureRecognizer(target: self,action: #selector(CustomSwitch.tapped))
    self.addGestureRecognizer(tapGest)
    
    panGest = UIPanGestureRecognizer(target: self,action: #selector(CustomSwitch.panned))
    button.addGestureRecognizer(panGest)
}


//MARK:- GestureRecognizers

@objc func tapped() {

    switchDirection()
}

@objc func panned(_ panRecognizer: UIPanGestureRecognizer) {
    
    let translation = panRecognizer.translation(in: self).x
  
    switch panRecognizer.state {
    
    case .changed:
        if translation >= -13 && translation <= 13 {
            if minMaxButtonConstraint + translation <= 13 {
                buttonLeadingConstraint.constant = minMaxButtonConstraint + translation
        }
    }
        
    case .ended:
        switchDirection()
        
    default:
        print("Default")
    }
    
}

@objc func switchDirection() {
    self.layoutIfNeeded()

    if isSwitched {

        buttonLeadingConstraint.constant = 4
        minMaxButtonConstraint = 0
        self.backgroundColor = .switchGrey
        isSwitched = false
    }
    else {
        
        buttonLeadingConstraint.constant = 14
        minMaxButtonConstraint = 14
        self.backgroundColor = .custompurple
        isSwitched = true
    }
    
    
    let switchButton = UIViewPropertyAnimator(duration: 0.4,curve: .easeInOut) {
        self.layoutIfNeeded()
    }
    
    switchButton.startAnimation()
    
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

我使用这个 customSwitch 作为组件的类基本上是一个 UITabelViewCell 类。

class SpaceDetailTableViewCell: UITableViewCell {

var typeLabel: UILabel = {
    let lbl = UILabel()
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.text = "Roof Light"
    lbl.font = UIFont.systemFont(ofSize: 16,weight: .medium)
    return lbl
}()
-----------------------------------------------------------------------
-----How do i get tap and Pan gestures from this switchButton???------
-----------------------------------------------------------------------
var switchBtn: CustomSwitch = {
    let swtch = CustomSwitch()
    swtch.translatesAutoresizingMaskIntoConstraints = false
    return swtch
}()

var onOffLabel: UILabel = {
    let lbl = UILabel()
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.text = "Off / On"
    lbl.font = UIFont.systemFont(ofSize: 14,weight: .regular)
    return lbl
}()

var intensityLabel: UILabel = {
    let lbl = UILabel()
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.text = "Intensity"
    lbl.font = UIFont.systemFont(ofSize: 14,weight: .regular)
    return lbl
}()

var imageOfDevice: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.contentMode = .scaleAspectFill
    v.image = #imageLiteral(resourceName: "lightimage")
    return v
}()

var intensityContainer: Container = {
    let v = Container()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .custompurple
    v.layer.cornerRadius = 10
    v.clipsToBounds = true  //Because cornerRadius wasn't working
    return v
}()

var percentageLabel: UILabel = {
    let lbl = UILabel()
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.text = "65%"
    lbl.font = UIFont.systemFont(ofSize: 16,weight: .medium)
    lbl.textColor = .custompurple
    return lbl
}()

var separatorLine: UIView = {
    let v = UIView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .switchGrey
    return v
}()
    
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
    super.init(style: .default,reuseIdentifier: reuseIdentifier)

    setupCell()
    addLinesInIntensityContainer(View: intensityContainer)
    
    
    let panGest = UIPanGestureRecognizer(target: self,action: #selector(swipeupdown(_:)))
    intensityContainer.addGestureRecognizer(panGest)
    
}

func setupCell() {
    contentView.addSubview(typeLabel)
    contentView.addSubview(switchBtn)
    contentView.addSubview(onOffLabel)
    contentView.addSubview(intensityLabel)
    contentView.addSubview(imageOfDevice)
    contentView.addSubview(intensityContainer)
    contentView.addSubview(percentageLabel)
    contentView.addSubview(separatorLine)
    
    
    NSLayoutConstraint.activate([
    
        typeLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 25),typeLabel.topAnchor.constraint(equalTo: contentView.topAnchor,//SwitchButton
        switchBtn.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: -40),switchBtn.topAnchor.constraint(equalTo: typeLabel.topAnchor),switchBtn.widthAnchor.constraint(equalToConstant: 32),switchBtn.heightAnchor.constraint(equalToConstant: 20),//OnOffLabel
        onOffLabel.centerXAnchor.constraint(equalTo: switchBtn.centerXAnchor),onOffLabel.topAnchor.constraint(equalTo: switchBtn.bottomAnchor,//IntensityLabel
        intensityLabel.centerXAnchor.constraint(equalTo: onOffLabel.centerXAnchor),intensityLabel.topAnchor.constraint(equalTo: onOffLabel.bottomAnchor,constant: 10),//ImageView
        imageOfDevice.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,imageOfDevice.topAnchor.constraint(equalTo: typeLabel.bottomAnchor,imageOfDevice.widthAnchor.constraint(equalToConstant: 180),imageOfDevice.heightAnchor.constraint(equalToConstant: 215),imageOfDevice.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant: -12),//IntensityContainer
        intensityContainer.trailingAnchor.constraint(equalTo: separatorLine.trailingAnchor),intensityContainer.topAnchor.constraint(equalTo: intensityLabel.bottomAnchor,intensityContainer.widthAnchor.constraint(equalToConstant: 65),intensityContainer.heightAnchor.constraint(equalToConstant: 140),//PercentageLabel
        percentageLabel.centerXAnchor.constraint(equalTo: intensityContainer.centerXAnchor),percentageLabel.topAnchor.constraint(equalTo: intensityContainer.bottomAnchor,//SeparatorLine
        separatorLine.leadingAnchor.constraint(equalTo: imageOfDevice.leadingAnchor),separatorLine.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: -25),separatorLine.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant: -1),separatorLine.heightAnchor.constraint(equalToConstant: 1)
        
    ])
}


@objc func swipeupdown(_ panRecognizer: UIPanGestureRecognizer) {
    
    let translation = panRecognizer.translation(in: self).y
    
    switch panRecognizer.state {
    
    case .began:
                     intensityContainer.setNeedsdisplay()
    
    case .changed:
                    if translation > 0 && translation < 140 {
                        Translation.translationInContainer = translation
                        intensityContainer.setNeedsdisplay()
                    }
                   
    case .ended:
                    Translation.translationEndedInContainer = translation
    default:
                    print("Default")
    }
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。