gpt4 book ai didi

swift - 如何在 UIStackView Swift 4 中查找所选按钮的索引

转载 作者:行者123 更新时间:2023-11-30 10:38:08 24 4
gpt4 key购买 nike

我正在尝试在堆栈 View 中查找所选按钮的索引。目前,我正在尝试通过在按钮上使用标签来做到这一点。最终目标是当长按手势停止时,选择堆栈 View 上的任何值,但目前,我的情况开关正在打印所有值,而不是所选值。我使用它而不是 collectionView 或 tableView,因为我发现它更容易实现动画。

按钮设置

let likeButton = UIButton(type: .system)
likeButton.setImage(#imageLiteral(resourceName: "blue_like").withRenderingMode(.alwaysOriginal), for: .normal)
likeButton.tag = 0
let heartButton = UIButton(type: .system)
heartButton.setImage(#imageLiteral(resourceName: "red_heart").withRenderingMode(.alwaysOriginal), for: .normal)
heartButton.tag = 1
let wowButton = UIButton(type: .system)
wowButton.setImage(#imageLiteral(resourceName: "surprised").withRenderingMode(.alwaysOriginal), for: .normal)
wowButton.tag = 2
let laughButton = UIButton(type: .system)
laughButton.setImage(#imageLiteral(resourceName: "cry_laugh").withRenderingMode(.alwaysOriginal), for: .normal)
laughButton.tag = 3
let sadButton = UIButton(type: .system)
sadButton.setImage(#imageLiteral(resourceName: "cry").withRenderingMode(.alwaysOriginal), for: .normal)
sadButton.tag = 4
let angryButton = UIButton(type: .system)
angryButton.setImage(#imageLiteral(resourceName: "angry").withRenderingMode(.alwaysOriginal), for: .normal)
angryButton.tag = 5
let buttons = [likeButton, heartButton, wowButton, laughButton, sadButton, angryButton]

长按手势结束

else if gesture.state == .ended {

// clean up the animation
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
let stackView = self.iconsContainerView.subviews.first
stackView?.subviews.forEach({ (button) in
button.transform = .identity
switch button.tag {
case 0:
print("like button")
case 1:
print("Heart")
case 2:
print("Wow")
case 3:
print("Laugh")
case 4:
print("Sad")
case 5:
print("Angry")
default:
fatalError("Oops, this should not happen")
}
})

self.iconsContainerView.transform = self.iconsContainerView.transform.translatedBy(x: 0, y: 50)
self.iconsContainerView.alpha = 0


}, completion: { (_) in
self.iconsContainerView.removeFromSuperview()
})


}

最佳答案

您最好的选择可能是为每个按钮附加一个长按手势,并在回调中检查 gesture.view

参见:UIButton Long Press Event

关于swift - 如何在 UIStackView Swift 4 中查找所选按钮的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57454600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com