如何解决向属性观察器中的对象添加平移手势
现在我的代码使用了属性观察器和堆栈视图的组合。我现在想要做的是为每个蓝色对象添加一个平移手势,您可以在下面的 gif 中看到。您可以在下面的 gif 中看到下面 gif 中的蓝色对象是什么。对象是蓝色按钮。我尝试将平移手势添加到 t_button 但我无法这样做,因为它超出了范围并且将其移出它所在的方法时。它会影响按钮的显示方式。我只想让蓝色物体移动。
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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。