gpt4 book ai didi

ios - UserInteraction Enabled 但 UIElement 仍然没有反应

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

enter image description here所以基本上我有一个评论单元格,其中包含几个元素,特别是 UIImageView,其中包含用户的个人资料图片。我在该元素上启用了 userInteraction,甚至添加了一个 tapGesture。但是,控制我要在此元素上运行的操作的函数仍然没有反应。

import Foundation
import UIKit

protocol CommentCellDelegate: class {
func optionsButtonTapped(cell: CommentCell)
}
class CommentCell: UICollectionViewCell {
weak var delegate: CommentCellDelegate? = nil
weak var newCommentController: NewCommentsViewController?
override var reuseIdentifier : String {
get {
return "cellID"
}
set {
// nothing, because only red is allowed
}
}
var didTapOptionsButtonForCell: ((CommentCell) -> Void)?

var comment: CommentGrabbed?{
didSet{
guard let comment = comment else{
return
}
// print("apples")
// textLabel.text = comment.content
//shawn was also here
profileImageView.loadImage(urlString: comment.user.profilePic!)
// print(comment.user.username)
let attributedText = NSMutableAttributedString(string: comment.user.username!, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14)])

attributedText.append(NSAttributedString(string: " " + (comment.content), attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]))
textView.attributedText = attributedText


}
}

let textView: UITextView = {
let textView = UITextView()
textView.font = UIFont.systemFont(ofSize: 14)
textView.isScrollEnabled = false
textView.isEditable = false
return textView
}()


let profileImageView: CustomImageView = {
let iv = CustomImageView()
iv.clipsToBounds = true
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleProfileTransition)))
iv.contentMode = .scaleAspectFill
return iv
}()

lazy var flagButton: UIButton = {
let flagButton = UIButton(type: .system)
flagButton.setImage(#imageLiteral(resourceName: "icons8-Info-64"), for: .normal)
flagButton.addTarget(self, action: #selector(optionsButtonTapped), for: .touchUpInside)
return flagButton
}()

@objc func optionsButtonTapped (){
didTapOptionsButtonForCell?(self)
}

@objc func onOptionsTapped() {
delegate?.optionsButtonTapped(cell: self)
}
@objc func handleProfileTransition(tapGesture: UITapGestureRecognizer){
print("Tapped Image Cell")
}



override init(frame: CGRect){
super.init(frame: frame)
addSubview(textView)
addSubview(profileImageView)
addSubview(flagButton)
textView.anchor(top: topAnchor, left: profileImageView.rightAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 4, paddingBottom: 4, paddingRight: 4, width: 0, height: 0)
profileImageView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40)
profileImageView.layer.cornerRadius = 40/2
flagButton.anchor(top: topAnchor, left: nil, bottom: nil, right: rightAnchor, paddingTop: 4, paddingLeft: 0, paddingBottom: 0, paddingRight: 4, width: 40, height: 40)
flagButton.addTarget(self, action: #selector(CommentCell.onOptionsTapped), for: .touchUpInside)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}




}

有什么我可能做错的想法吗?用户界面层次结构

最佳答案

在 self 完全初始化之前,您正在尝试将手势识别器目标设置为 self。

考虑对您的 profileImageView 使用惰性初始化。

它应该是这样的:

lazy var profileImageView: CustomImageView = {
let iv = CustomImageView()
iv.clipsToBounds = true
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleProfileTransition)))
iv.contentMode = .scaleAspectFill
return iv
}()

关于ios - UserInteraction Enabled 但 UIElement 仍然没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48473529/

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