swift - 向属性观察器中的对象添加平移手势
问题描述
现在我的代码结合了属性观察器和堆栈视图。我现在要做的是为您在下面的 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
}
}
解决方案
推荐阅读
- xmpp - 如何防止用户向群组发送消息,但继续接收来自该群组的消息(只读)?
- python - 无法使用 tf.control_dependencies() 创建控制依赖项
- python - Python 异常在 Glib/DBUS 循环中发生时不会传播/打印
- django - 如何定义 django 模板文件夹?
- python - 可以用 PyCharm Professional 代替 CLion - 主要用于 python
- android - MacBook Pro Mojave、Android Pie、Android 设备被 adb 识别但未被 Android Studio 识别
- css - 如何弯曲 CSS 伪元素背景?
- javascript - Nest Js TypeORM repository.update 方法不更改对象的多个字段
- angular - 输入子组件给出错误,但有时会加载
- reactjs - 如何使用 React + Context API + useReducer Hook + RxJS observables 过滤每个动作类型并异步执行其他动作 + LIVE DEMO