gpt4 book ai didi

ios - 滑动时单个标签更新值

转载 作者:行者123 更新时间:2023-11-28 07:48:11 25 4
gpt4 key购买 nike

由于一些强大的帮助,我现在可以在各个标签上使用滑动功能了!现在我只需要这些标签中的每一个在滑动时单独更新,因此每个标签都有自己单独的计数器并且不会影响其他标签。

这是我的代码:

导入 UIKit

class ViewController: UIViewController {

var counter = 0

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!

var counters: [UILabel: Int] = [:]

override func viewDidLoad() {
super.viewDidLoad()

for label: UILabel in [label1, label2, label3] {

counters[label] = 0

for direction: UISwipeGestureRecognizerDirection in [.up, .down, .left, .right] {

let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(didSwipe(_:)))
swipeGesture.direction = direction
label.addGestureRecognizer(swipeGesture)
label.isUserInteractionEnabled = true
label.isMultipleTouchEnabled = true
}
}
}

@objc func didSwipe(_ gestureRecognizer: UISwipeGestureRecognizer) {



guard let label = gestureRecognizer.view as? UILabel else { return }

debugPrint("\(gestureRecognizer.direction)")

switch gestureRecognizer.direction {

case .up:
counters[label] = counters[label]! + 5
print(counters)

case .down:
counters[label] = 0
print(counters)

case .left:
counters[label] = counters[label]! - 1
print(counters)

case .right:
counters[label] = counters[label]! + 1
print(counters)

default:
label.text = "0"
}
}

最佳答案

你需要打电话

label.text = "\(counters[label]!)"

在 switch 语句的末尾。这应该有效。

关于ios - 滑动时单个标签更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50401719/

25 4 0