将按钮添加到滚动视图中的按钮数组
add button to a array of button in a scrollview
我想在 ui 按钮的数组中添加另一个 Uibbuton。当在滚动视图上调用 func 时,它不会被添加。正如您在下面的 gif 中看到的,当按下添加按钮时,没有添加任何内容。我想做的是当按下添加按钮时,添加一个按钮标题 Button 10。由于按钮是在视图中初始化的,所以我不知道该怎么做?
import UIKit
class SwipeableUIScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UIButton || view is UILabel{
return true
}
return touchesShouldCancel(in: view)
}
}
class ViewController: UIViewController {
var scrollView:SwipeableUIScrollView!
var greenView:UIView!
var addMore:UIButton!
var counter = 10
var scrollViewHeightConstraint:NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
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)
self.view.addSubview(scrollView)
self.view.addSubview(greenView)
self.view.addSubview(addMore)
addMore.addTarget(self, action: #selector(increaseC), for: .touchDown)
scrollViewHeightConstraint = NSLayoutConstraint.init(item: scrollView, attribute: .height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)
NSLayoutConstraint.activate([
addMore.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50.0),
addMore.trailingAnchor.constraint(equalTo: self.view.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: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollViewHeightConstraint,
greenView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
greenView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
greenView.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
greenView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
var leadingAnchor = self.scrollView!.leadingAnchor
for i in 0..<counter{
let t_button = UIButton.init(frame: CGRect.zero)
t_button.translatesAutoresizingMaskIntoConstraints = false
t_button.backgroundColor = UIColor.blue
scrollView.addSubview(t_button)
NSLayoutConstraint.activate([
t_button.leadingAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
t_button.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
t_button.heightAnchor.constraint(equalToConstant: 20),
t_button.widthAnchor.constraint(equalToConstant: 75.0)
])
leadingAnchor = t_button.trailingAnchor
t_button.setTitle("Button \(i)", for: .normal)
}
self.scrollView.trailingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
@objc func increaseC(){
counter += 1
}
}
你可以使用Property Observer
。属性观察器允许我们在使用属性时希望属性改变时执行某些操作。也就是willSet之前运行或者willSet之后didSet的一些小代码块属性可以改。创建属性观察者,在属性后加花括号(类似计算属性的做法)
就像这样:
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()
observer.number = 4
// About to change to 4 ,Just changed from 0 to 4
所以你的例子可以修改成这样
import UIKit
class SwipeableUIScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UIButton || view is UILabel{
return true
}
return touchesShouldCancel(in: view)
}
}
class ViewController: UIViewController {
var scrollView:SwipeableUIScrollView!
var greenView:UIView!
var addMore:UIButton!
var leadingAnchor: NSLayoutXAxisAnchor!
var counter: Int? {
didSet {
for i in (oldValue ?? 0)..<(counter ?? 0){
let t_button = UIButton.init(frame: CGRect.zero)
t_button.translatesAutoresizingMaskIntoConstraints = false
t_button.backgroundColor = UIColor.blue
scrollView.addSubview(t_button)
NSLayoutConstraint.activate([
t_button.leadingAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
t_button.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
t_button.heightAnchor.constraint(equalToConstant: 20),
t_button.widthAnchor.constraint(equalToConstant: 75.0)
])
leadingAnchor = t_button.trailingAnchor
t_button.setTitle("Button \(i)", for: .normal)
}
}
}
var scrollViewHeightConstraint:NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
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)
self.view.addSubview(scrollView)
self.view.addSubview(greenView)
self.view.addSubview(addMore)
addMore.addTarget(self, action: #selector(increaseC), for: .touchDown)
scrollViewHeightConstraint = NSLayoutConstraint.init(item: scrollView, attribute: .height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)
NSLayoutConstraint.activate([
addMore.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50.0),
addMore.trailingAnchor.constraint(equalTo: self.view.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: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollViewHeightConstraint,
greenView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
greenView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
greenView.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
greenView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
leadingAnchor = self.scrollView!.leadingAnchor
counter = 10
self.scrollView.trailingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
@objc func increaseC(){
counter! += 1
}
}
我想在 ui 按钮的数组中添加另一个 Uibbuton。当在滚动视图上调用 func 时,它不会被添加。正如您在下面的 gif 中看到的,当按下添加按钮时,没有添加任何内容。我想做的是当按下添加按钮时,添加一个按钮标题 Button 10。由于按钮是在视图中初始化的,所以我不知道该怎么做?
import UIKit
class SwipeableUIScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UIButton || view is UILabel{
return true
}
return touchesShouldCancel(in: view)
}
}
class ViewController: UIViewController {
var scrollView:SwipeableUIScrollView!
var greenView:UIView!
var addMore:UIButton!
var counter = 10
var scrollViewHeightConstraint:NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
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)
self.view.addSubview(scrollView)
self.view.addSubview(greenView)
self.view.addSubview(addMore)
addMore.addTarget(self, action: #selector(increaseC), for: .touchDown)
scrollViewHeightConstraint = NSLayoutConstraint.init(item: scrollView, attribute: .height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)
NSLayoutConstraint.activate([
addMore.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50.0),
addMore.trailingAnchor.constraint(equalTo: self.view.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: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollViewHeightConstraint,
greenView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
greenView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
greenView.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
greenView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
var leadingAnchor = self.scrollView!.leadingAnchor
for i in 0..<counter{
let t_button = UIButton.init(frame: CGRect.zero)
t_button.translatesAutoresizingMaskIntoConstraints = false
t_button.backgroundColor = UIColor.blue
scrollView.addSubview(t_button)
NSLayoutConstraint.activate([
t_button.leadingAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
t_button.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
t_button.heightAnchor.constraint(equalToConstant: 20),
t_button.widthAnchor.constraint(equalToConstant: 75.0)
])
leadingAnchor = t_button.trailingAnchor
t_button.setTitle("Button \(i)", for: .normal)
}
self.scrollView.trailingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
@objc func increaseC(){
counter += 1
}
}
你可以使用Property Observer
。属性观察器允许我们在使用属性时希望属性改变时执行某些操作。也就是willSet之前运行或者willSet之后didSet的一些小代码块属性可以改。创建属性观察者,在属性后加花括号(类似计算属性的做法)
就像这样:
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()
observer.number = 4
// About to change to 4 ,Just changed from 0 to 4
所以你的例子可以修改成这样
import UIKit
class SwipeableUIScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UIButton || view is UILabel{
return true
}
return touchesShouldCancel(in: view)
}
}
class ViewController: UIViewController {
var scrollView:SwipeableUIScrollView!
var greenView:UIView!
var addMore:UIButton!
var leadingAnchor: NSLayoutXAxisAnchor!
var counter: Int? {
didSet {
for i in (oldValue ?? 0)..<(counter ?? 0){
let t_button = UIButton.init(frame: CGRect.zero)
t_button.translatesAutoresizingMaskIntoConstraints = false
t_button.backgroundColor = UIColor.blue
scrollView.addSubview(t_button)
NSLayoutConstraint.activate([
t_button.leadingAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
t_button.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
t_button.heightAnchor.constraint(equalToConstant: 20),
t_button.widthAnchor.constraint(equalToConstant: 75.0)
])
leadingAnchor = t_button.trailingAnchor
t_button.setTitle("Button \(i)", for: .normal)
}
}
}
var scrollViewHeightConstraint:NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
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)
self.view.addSubview(scrollView)
self.view.addSubview(greenView)
self.view.addSubview(addMore)
addMore.addTarget(self, action: #selector(increaseC), for: .touchDown)
scrollViewHeightConstraint = NSLayoutConstraint.init(item: scrollView, attribute: .height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)
NSLayoutConstraint.activate([
addMore.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50.0),
addMore.trailingAnchor.constraint(equalTo: self.view.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: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollViewHeightConstraint,
greenView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
greenView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
greenView.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
greenView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
leadingAnchor = self.scrollView!.leadingAnchor
counter = 10
self.scrollView.trailingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
@objc func increaseC(){
counter! += 1
}
}