Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 向“特性观察者”中的对象添加平移手势_Swift_Loops_Uipangesturerecognizer_Didset_Property Observer - Fatal编程技术网

Swift 向“特性观察者”中的对象添加平移手势

Swift 向“特性观察者”中的对象添加平移手势,swift,loops,uipangesturerecognizer,didset,property-observer,Swift,Loops,Uipangesturerecognizer,Didset,Property Observer,现在,我的代码使用属性观察者和堆栈视图的组合。我现在要做的是,在下面的gif中看到的每个蓝色对象上添加一个平移手势。您可以在下面的gif中看到下面gif中的蓝色对象。对象是蓝色按钮。我尝试将平移手势添加到t_按钮,但我无法这样做,因为它超出了范围,并且在将其移出所使用的方法时。它影响按钮的显示方式。我只想让蓝色的物体移动 import UIKit class PropertyObserverExmple { var number: Int = 0 { willSet(ne

现在,我的代码使用属性观察者和堆栈视图的组合。我现在要做的是,在下面的gif中看到的每个蓝色对象上添加一个平移手势。您可以在下面的gif中看到下面gif中的蓝色对象。对象是蓝色按钮。我尝试将平移手势添加到t_按钮,但我无法这样做,因为它超出了范围,并且在将其移出所使用的方法时。它影响按钮的显示方式。我只想让蓝色的物体移动

import UIKit
class PropertyObserverExmple {
    var number: Int = 0 {
        willSet(newNumber) {
            print("About to change to \(newNumber)")
        }
        didSet(oldNumber) {
            print("Just changed from \(oldNumber) to \(self.number)")
        }
    }
}

var observer = PropertyObserverExmple()

class SwipeableUIScrollView: UIScrollView {
    
    override func touchesShouldCancel(in view: UIView) -> Bool {
        
        if view is UIButton || view is UILabel{
            return true
        }
        
        return touchesShouldCancel(in: view)
    }
    
}

class ScrollButtonsViewController: UIViewController {
    
    // this will hold the buttons
    var stackView: UIStackView!
    
    var scrollView:SwipeableUIScrollView!
    var greenView:UIView!
    
    var addMore:UIButton!
    var leadingAnchor: NSLayoutXAxisAnchor!
    
    var mover = UIPanGestureRecognizer()
    
    var counter: Int? {
        didSet {
            for i in (oldValue ?? 0)..<(counter ?? 0) {
                
                let t_button = UIButton()
                t_button.translatesAutoresizingMaskIntoConstraints = false
                
                t_button.backgroundColor = UIColor.blue
                
                
                stackView.addArrangedSubview(t_button)
                
             
                
                NSLayoutConstraint.activate([
                    t_button.heightAnchor.constraint(equalToConstant: 20),
                    t_button.widthAnchor.constraint(equalToConstant: 75.0)
                ])
                
                t_button.setTitle("Button \(i)", for: .normal)
                
                let gesture = UIPanGestureRecognizer(target: self, action: #selector(addGG(_:)))
                
                t_button.addGestureRecognizer(gesture)
                
            }
        }
        
        
    }
    @objc func addGG(_ sender: UIPanGestureRecognizer){
        let point = sender.location(in: view)
        
        
        
        
    }

    var scrollViewHeightConstraint:NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        observer.number = 4
        scrollView = SwipeableUIScrollView.init(frame: CGRect.zero)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        
        greenView = UIView.init(frame: CGRect.zero)
        greenView.translatesAutoresizingMaskIntoConstraints = false
        greenView.backgroundColor = UIColor.green
        
        addMore = UIButton.init(frame: CGRect.zero)
        addMore.translatesAutoresizingMaskIntoConstraints = false
        addMore.backgroundColor = UIColor.orange
        addMore.setTitle("add", for: .normal)
        addMore.setTitleColor(.white, for: .normal)
        addMore.setTitleColor(.black, for: .highlighted)
        
        self.view.addSubview(scrollView)
        self.view.addSubview(greenView)
        self.view.addSubview(addMore)
        
        addMore.addTarget(self, action: #selector(increaseC), for: .touchDown)

        // respect safe area
        let g = view.safeAreaLayoutGuide
        
        scrollViewHeightConstraint = scrollView.heightAnchor.constraint(equalToConstant: 50.0)
        
        NSLayoutConstraint.activate([
            
            addMore.topAnchor.constraint(equalTo: g.topAnchor, constant: 50.0),
            addMore.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -15.0),
            addMore.heightAnchor.constraint(equalToConstant: 25.0),
            addMore.widthAnchor.constraint(equalToConstant: 100.0),
            
            scrollView.topAnchor.constraint(equalTo: self.addMore.bottomAnchor, constant: 10.0),
            scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            scrollViewHeightConstraint,
            
            greenView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            greenView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            greenView.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
            greenView.bottomAnchor.constraint(equalTo: g.bottomAnchor)
            
        ])
        
        stackView = UIStackView()
        stackView.axis = .horizontal    // the default, but just for clarity
        stackView.distribution = .fill  // the default, but just for clarity
        stackView.alignment = .center
        stackView.spacing = 5
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        // add the stack view to the scroll view
        scrollView.addSubview(stackView)
        
        // constrain the stack view
        //  this will ALSO define the "scrollable" area
        NSLayoutConstraint.activate([
            
            // constrain stack view to scroll view's Content Layout Guide
            stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
            stackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
            stackView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
            
            // constrain stack view height to scroll view's Frame Layout Guide
            stackView.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),
        ])
        
        counter = 10
        
    }
    
    @objc func increaseC(){
        counter! += 1
    }
    
}
导入UIKit
类属性ObserverExample{
变量编号:Int=0{
willSet(新号码){
打印(“即将更改为\(新编号)”)
}
didSet(旧号码){
打印(“刚从\(旧号码)更改为\(self.number)”)
}
}
}
var observer=PropertyObserverExample()
类SwipeableUIScrollView:UIScrollView{
覆盖功能触摸应取消(在视图中:UIView)->Bool{
如果视图为UIButton | |视图为UILabel{
返回真值
}
返回触摸应取消(在:视图中)
}
}
类ScrollButtonsViewController:UIViewController{
//这将保持按钮
var stackView:UIStackView!
var scrollView:SwipeableUIScrollView!
var greenView:UIView!
var addMore:ui按钮!
var leadingAnchor:NSLayoutXAxisAnchor!
var mover=uipangestrerecognizer()
变量计数器:Int{
迪塞特{
对于i in(旧值??0)。。