以编程方式创建 UIView 后滑块没有响应

Posted

技术标签:

【中文标题】以编程方式创建 UIView 后滑块没有响应【英文标题】:Slider not responsive after creating UIView programmatically 【发布时间】:2019-02-12 07:55:15 【问题描述】:

我以编程方式创建了一个UIView,并使用PureLayout 放置了一个UIImageView init 来约束UI。

然而,我在 UI 上附加了一个滑块功能,现在它工作正常,当我决定以编程方式实现 UIView 时,滑块功能不起作用。我不知道为什么,但这是我能够做到的

protocol SwipeDelegate: class 
    func swiped(for view: TripView)


class TripView: UIView 

    var shouldSetupConstraints = true
    var startingFrame: CGRect?
    weak var swipeDelegate: SwipeDelegate?

    let screenSize = UIScreen.main.bounds

    let sliderView: UIView = UIImageView(frame: CGRect.zero)
    let sliderImage: UIImageView = UIImageView(frame: CGRect.zero)

    override init(frame: CGRect)
        super.init(frame: frame)

        commonInit()
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)

        commonInit()
    

    private func commonInit() 
        sliderView.backgroundColor = UIColor.green
        sliderView.autoSetDimension(.height, toSize: screenSize.width / 6)

        self.addSubview(sliderView)

        sliderImage.backgroundColor = UIColor.clear
        sliderImage.image = UIImage(named: "icons8-double_right_filled.png")
        sliderImage.contentMode = .scaleAspectFit

        sliderImage.autoSetDimension(.width, toSize: screenSize.width / 6)
        sliderImage.autoSetDimension(.height, toSize: screenSize.width / 6)

        self.addSubview(sliderImage)

        swipeFunc()
    

    override func updateConstraints() 
        if(shouldSetupConstraints) 

            sliderView.autoPinEdgesToSuperviewSafeArea(with: UIEdgeInsets.zero, excludingEdge: .bottom)
            sliderImage.autoPinEdge(toSuperviewEdge: .left)
            sliderImage.autoPinEdge(.bottom, to: .bottom, of: sliderView, withOffset: 0.0)

            shouldSetupConstraints = false
        

        super.updateConstraints()
    


extension TripView 

    private func swipeFunc() 

        let swipeGesture = UIPanGestureRecognizer(target: self, action: #selector(acknowledgeSwiped(sender:)))
        sliderImage.addGestureRecognizer(swipeGesture)
        sliderImage.isUserInteractionEnabled = true
        swipeGesture.delegate = self as? UIGestureRecognizerDelegate
    

    @objc func acknowledgeSwiped(sender: UIPanGestureRecognizer) 
        if let sliderView = sender.view 
            let translation = sender.translation(in: sliderView)
            switch sender.state 
            case .began:
                startingFrame = sliderImage.frame
                fallthrough
            case .changed:
                if let startFrame = startingFrame 

                    var movex = translation.x
                    if movex < -startFrame.origin.x  movex = -startFrame.origin.x 

                    let xMax = sliderView.frame.width - startFrame.origin.x - startFrame.width
                    if movex > xMax 
                        movex = xMax
                        swipeDelegate?.swiped(for: self)
                        //                        dismiss(animated: true, completion: nil)
                    

                    var movey = translation.y
                    if movey < -startFrame.origin.y  movey = -startFrame.origin.y 

                    let yMax = sliderView.frame.height - startFrame.origin.y - startFrame.height
                    if movey > yMax 
                        movey = yMax

                    

                    sliderView.transform = CGAffineTransform(translationX: movex, y: movey)
                
            default: // .ended and others:
                UIView.animate(withDuration: 0.1, animations: 
                    sliderView.transform = CGAffineTransform.identity
                )
            
        
    


任何帮助将不胜感激

【问题讨论】:

你的 acknowledgeSwiped 被调用了吗? 【参考方案1】:

请看一下你的实例变量名,它们被acknowledgeSwiped() 中的局部变量所取代。 保持变量名称不同。

@objc func acknowledgeSwiped(sender: UIPanGestureRecognizer) 
    if let sliderView = sender.view 
        let translation = sender.translation(in: self.sliderView) //self.sliderView
        switch sender.state 
        case .began:
            startingFrame = sliderImage.frame
            fallthrough
        case .changed:
            if let startFrame = startingFrame 

                var movex = translation.x
                if movex < -startFrame.origin.x  movex = -startFrame.origin.x 

                let xMax = self.sliderView.frame.width - startFrame.origin.x - startFrame.width //self.sliderView
                if movex > xMax 
                    movex = xMax
                    swipeDelegate?.swiped(for: self)
                    //                        dismiss(animated: true, completion: nil)
                

                var movey = translation.y
                if movey < -startFrame.origin.y  movey = -startFrame.origin.y 

                let yMax = self.sliderView.frame.height - startFrame.origin.y - startFrame.height //self.sliderView
                if movey > yMax 
                    movey = yMax

                

                sliderView.transform = CGAffineTransform(translationX: movex, y: movey)
            
        default: // .ended and others:
            UIView.animate(withDuration: 0.1, animations: 
                sliderView.transform = CGAffineTransform.identity
            )
        
    

【讨论】:

使用 self.view.addSubview(....) 完全阻止我与主 UI 交互 非常感谢冠军。

以上是关于以编程方式创建 UIView 后滑块没有响应的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式通过 uibutton 删除 uiview

更改曲目 NAudio 后滑块不起作用

以编程方式创建的 UIView 的框架为 0

从 UIView 以编程方式创建 UIImage

没有帖子时以编程方式显示 UIView

以编程方式对子视图进行约束