我有一个自定义 uibutton,在自定义 uibutton 中,我传递了一个带有图像名称的 viewModel,它只调用了 setImage。addTarget 函数不起作用。我在这里缺少什么我试图禁用视图上的用户交互因为我在它上面设置了一个图像视图。
CustomButton.swift
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
clipsToBounds = true
layer.cornerRadius = 9
layer.borderWidth = 0
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(with viewModel: CustomButtonViewModel ){
translatesAutoresizingMaskIntoConstraints = false
setImage(UIImage(named: viewModel.image), for: .normal)
self.imageView?.contentMode = .scaleToFill
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
struct CustomButtonViewModel {
let image : String
}
ViewController.swift
class ViewController: UIViewController {
//Spinner
// intiate uibuttons
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView(image: UIImage(named: "bg.png"))
imageView.isUserInteractionEnabled = false
view = imageView
let dogsButton = CustomButton(frame: CGRect(x: 20, y: 100, width: 140, height: 130))
let catsButton = CustomButton(frame: CGRect(x: 200, y: 100, width: 140, height: 130))
dogsButton.configure(with: CustomButtonViewModel(image: "dogs.png"))
catsButton.configure(with: CustomButtonViewModel(image: "cats.png"))
dogsButton.addTarget(self, action: #selector(dogsButtonClicked(sender:)), for: .touchUpInside)
catsButton.addTarget(self, action: #selector(catsButtonClicked), for: .touchUpInside)
view.addSubview(dogsButton)
view.addSubview(catsButton)
}
@objc func dogsButtonClicked(sender : UIButton){
print("dogs button clicked")
// let vc = CollectionViewController()
// let navVC = UINavigationController(rootViewController: vc)
// navVC.modalPresentationStyle = .fullScreen
// present(navVC, animated: true)
}
@objc func catsButtonClicked(){
let vc = CollectionViewController()
navigationController?.pushViewController(vc, animated: true)
}
}
答案 0 :(得分:0)
您正在将 const [activeIndex, setactiveIndex] = useState([]);
const [_, activator] = useState(-1); //If I add this line of code the array gets updated in time
const ModifyActiveIndex = (index) => {
activator(index);
if (!activeIndex.includes(index)) {
const copy = activeIndex;
copy.push(index);
setactiveIndex(copy);
} else {
const filtercopy = activeIndex.filter((item) => {
return item !== index;
});
setactiveIndex(filtercopy);
}
};
const checkEntityactive = (index) => {
return activeIndex.includes(index);
};
const ProductList = temp.map((element, index) => {
return (
<ListItem key={index}>
<Checkbox
checked={checkEntityactive(index)}
onClick={() => ModifyActiveIndex(index)}
/>
</ListItem>
);
});
分配给 UIImageView
的基本视图并将其 UIViewController
属性设置为 isUserInteractionEnabled
,这将禁用其所有子视图的用户交互,包括你的按钮。您只需将此属性设置为 true,触摸事件就会起作用:
false