使选定的uiview随着滑块移动而增加尺寸

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

在我的代码中,当用户点击 rectB 按钮时,它会添加小 uiviews。当用户选择特定视图时,它应该根据滑块增大或减小尺寸。当我点击特定框足够长的时间时,下面的代码有时会起作用,但现在不起作用。我已经添加了我正在寻找的内容的 gif。这张 gif 并不代表我下面的核心内容。这是所需的输出。

enter image description here

import UIKit

class ViewController: UIViewController {
    
    var rectB = UIButton()
    
  
    
    var slider = UISlider()
    
    var arrTextFields = [UIImageView]()
    

    var ht = -90
    var ww = 80
    
    var widthOfSquare = 30
    var selectNum = Int()
    var currentView: UIView?
    
    
    var count: Int = 0
    


    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        slider.value = 0.50
        [rectB , slider,  ].forEach {
            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.layer.borderWidth = 1
            $0.backgroundColor = UIColor(
                red: .random(in: 0.5...0.7),
                green: .random(in: 0.0...1),
                blue: .random(in: 0.3...0.5),
                alpha: 1
            )
            
            $0.layer.borderWidth = 1
            
            if let textfield = $0 as? UITextField {
                textfield.textAlignment = .center
           
            }
            
            if let label = $0 as? UILabel {
                
                label.textAlignment = .center
            }
            if let button = $0 as? UIButton {
                button.setTitleColor(.black, for: .normal)
            }
            
        }//below
        
        
        
        NSLayoutConstraint.activate([
            
         
            
            
            slider.bottomAnchor.constraint(equalTo: rectB.topAnchor),
            slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.08, constant: 0),
            slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
            slider.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            
            
            
            
            
            
          
            
            rectB.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            rectB.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.08, constant: 0),
            rectB.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5, constant: 0),
            rectB.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            
          
        ])
        
        slider.addTarget(self, action: #selector(sxBTN), for: .valueChanged)
        
        rectB.addTarget(self, action: #selector(rectangeBox), for: .touchDown)
    
        
     
        
        
        
        
        
        rectB.setTitle("Rectangle", for: .normal)
        
     
        
    }
   

   
    
    
    @objc func handleTapGestured(_ gesture: UIPanGestureRecognizer) {
        currentView = gesture.view
    }
    
    @objc func sxBTN(sender : UISlider){
        
        
        
        if selectNum == 1 {
          
            
            currentView?.bounds.size.width = CGFloat(slider.value) /   (currentView?.bounds.size.width)! * 100
            currentView?.bounds.size.height = CGFloat(slider.value) / (currentView?.bounds.size.height)! * 100
            

            
            print("in 1")
            
            return
        }
        

        if selectNum == 2 {//opacity
            
            let diff = slider.maximumValue-slider.minimumValue
            
            
            currentView?.alpha = CGFloat(slider.value/diff)
            
            print("2")
            return
        }

        
        
        
    }
  
    
    @objc func rectangeBox(){
        selectNum = 1
        let subview = UIImageView()
        
        
        subview.isUserInteractionEnabled = true
        arrTextFields.append(subview)
        view.addSubview(subview)
        
        
        
        slider.addTarget(self, action: #selector(sxBTN(sender:)), for: .valueChanged)
        slider.minimumValue = 10
        slider.maximumValue = 200
        
        subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ww), width: CGFloat(ww), height: 35)
        subview.backgroundColor = UIColor.orange
        subview.tag = count
        let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
        
        
        subview.addGestureRecognizer(pan)
        
        count += 1
        ht += 50
        arrTextFields.append(subview)
        
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
        subview.addGestureRecognizer(tapGesture)
    }
    
 
    
    @objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
        
        
        let draggedView = gesture.view!
        view.bringSubviewToFront(draggedView)
        
        
        let translation = gesture.translation(in: view)
        draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
        gesture.setTranslation(.zero, in: view)
    }
    
}
swift uiview slider uipangesturerecognizer
1个回答
0
投票

您可以使用此代码。选择新矩形时,您可能需要重置滑块。

import UIKit

class ViewController: UIViewController {
    
    var rectB = UIButton()
    var slider = UISlider()
    
    var arrTextFields = [UIImageView]()
    

    var ht = -90
    var ww = 80
    
    var widthOfSquare = 30
    var selectNum = Int()
    var currentView: UIView?
    var count: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        slider.value = 0.50
        [rectB , slider].forEach {
            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.layer.borderWidth = 1
            $0.backgroundColor = UIColor(
                red: .random(in: 0.5...0.7),
                green: .random(in: 0.0...1),
                blue: .random(in: 0.3...0.5),
                alpha: 1
            )
            
            $0.layer.borderWidth = 1
            
            if let textfield = $0 as? UITextField {
                textfield.textAlignment = .center
           
            }
            
            if let label = $0 as? UILabel {
                
                label.textAlignment = .center
            }
            if let button = $0 as? UIButton {
                button.setTitleColor(.black, for: .normal)
            }
            
        }//below
        
        
        
        NSLayoutConstraint.activate([
            
            slider.bottomAnchor.constraint(equalTo: rectB.topAnchor),
            slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.08, constant: 0),
            slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
            slider.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            
            rectB.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            rectB.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.08, constant: 0),
            rectB.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5, constant: 0),
            rectB.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            
          
        ])
        
        slider.addTarget(self, action: #selector(sxBTN), for: .valueChanged)
        slider.minimumValue = 1
        slider.maximumValue = 3
        
        rectB.addTarget(self, action: #selector(rectangeBox), for: .touchDown)
        rectB.setTitle("Rectangle", for: .normal)
        
    }
    
    @objc func handleTapGestured(_ gesture: UIPanGestureRecognizer) {
        currentView = gesture.view
    }
    
    @objc func sxBTN(sender : UISlider){
        
        if selectNum == 1 {
          
            currentView?.bounds.size.width = CGFloat(ww) * CGFloat(slider.value)
            currentView?.bounds.size.height = CGFloat(35) * CGFloat(slider.value)
           
            print("in 1")
            
            return
        }
        

        if selectNum == 2 {//opacity
            
            let diff = slider.maximumValue-slider.minimumValue
            
            
            currentView?.alpha = CGFloat(slider.value/diff)
            
            print("2")
            return
        }
    }
  
    
    @objc func rectangeBox(){
        selectNum = 1
        let subview = UIImageView()
        
        subview.isUserInteractionEnabled = true
        arrTextFields.append(subview)
        view.addSubview(subview)
        
        subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ww), width: CGFloat(ww), height: 35)
        subview.backgroundColor = UIColor.orange
        subview.tag = count
        let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
        
        
        subview.addGestureRecognizer(pan)
        
        count += 1
        ht += 50
        arrTextFields.append(subview)
        
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
        subview.addGestureRecognizer(tapGesture)
    }
    
 
    
    @objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
        
        
        let draggedView = gesture.view!
        view.bringSubviewToFront(draggedView)
        
        
        let translation = gesture.translation(in: view)
        draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
        gesture.setTranslation(.zero, in: view)
    }
    
}


© www.soinside.com 2019 - 2024. All rights reserved.