【发布时间】:2020-10-03 00:43:44
【问题描述】:
我有一个按钮,点击后会变成复选标记。它适用于包括 X 和 XR 在内的所有模拟器,但不适用于实际的 X 或 XR 设备(根据使用 TestFlight 的朋友的说法)。不过,它适用于其他几种设备。想知道是否与 X 和 XR 有什么特别的关系,或者可能是限制,剪辑到界限?问题是它没有显示复选标记或将与其单元格关联的值添加到我拥有的数组中,所以我认为它在这些手机上是不可选的。
let button: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.masksToBounds = true
button.isUserInteractionEnabled = true
button.clipsToBounds = true
button.contentMode = .scaleAspectFit
return button
}()
约束
button.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
button.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -(width/40)).isActive = true
button.widthAnchor.constraint(equalToConstant: width/8).isActive = true
手势识别器
private func setupGestureRecognizer() {
let joinButtonTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(joinButtonTapped))
button.addGestureRecognizer(joinButtonTapGestureRecognizer)
}
@objc private func joinButtonTapped() {
delegate?.joinButtonTapped(inCell: self, button: button)
}
JoinButtonTapped -> 加号按钮到复选标记并将组名添加到数组
extension SearchGroupsVC: GroupsViewCellDelegate {
func joinButtonTapped(inCell cell: UITableViewCell, button: UIButton) {
guard let indexPath = tableView.indexPath(for: cell) else { return }
if searchBar.text?.count != 0 {
group = filteredTypes[indexPath.row]
} else {
group = allGroups[indexPath.row]
}
if selected.contains(group) {
if self.traitCollection.userInterfaceStyle == .dark {
button.setImage(UIImage(named: "plus_white"), for: .normal)
} else {
button.setImage(UIImage(named: "plus_black"), for: .normal)
}
selected.remove(at: selected.index(of: group)!)
} else if !selected.contains(group){
if self.traitCollection.userInterfaceStyle == .dark {
button.setImage(UIImage(named: "checkmark_white"), for: .normal)
} else {
button.setImage(UIImage(named: "checkmark_black"), for: .normal)
}
selected.append(group)
}
}
}
【问题讨论】:
-
您是否收到按钮的回调? (就像
#selector被调用?) -
不幸的是,我没有办法在手头的 X 或 XR 上对其进行测试。这是一些朋友在试用 TestFlight 版本时告诉我的。我只有 iPhone 7 和 Xcode 模拟器可以尝试,但这些都可以。
-
等等——为什么要在按钮上添加轻击手势识别器?
-
哦,错了吗?不确定我是否看到在教程中使用它。 Tap Tester 代码位于自定义视图单元格中。它适用于大多数手机。
-
一个按钮已经可以点击了,那么点击手势识别器是干什么用的?