我刚刚完成了大书呆子牧场的iOS编程书,并开始了我的第一个项目'。我试图使用UIImageView作为按钮,但我的图像显示为零,我无法弄清楚原因。我是新手,所以任何帮助,甚至只是识别这段代码中没有意义的任何部分都会受到赞赏。
import UIKit
class ImageScreenViewController: UIViewController {
@ IBOutlet var imageView: UIImageView!
// set up images
let picture1 = UIImage(named: "picture1")
override func viewDidLoad() {
super.viewDidLoad()
// set image on screen
imageView.image = picture1
}
// set up gesture recognizer
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(count(_:)))
self.imageView.addGestureRecognizer(tapRecognizer)
self.imageView.isUserInteractionEnabled = true
}
@objc func count(_ gestureRecognizer: UIGestureRecognizer) {
print("tapped the picture")
}
}
注意,picture1在Assets.xcassets中。
答案 0 :(得分:0)
你可以在viewDidload中添加tapRecongnize,因为在init方法中imageview没有初始化并返回nil所以你可以像这样使用
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = picture1
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(count(_:)))
self.imageView.addGestureRecognizer(tapRecognizer)
self.imageView.isUserInteractionEnabled = true
}
答案 1 :(得分:0)
对您的问题的一些解释
encodeWithCoder(_ aCoder: NSCoder) {
// Serialize
}
init(coder aDecoder: NSCoder) {
// Deserialize
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//You used
}
要添加手势,您实际上不需要在此处实现编码器,因为它们用于存储状态
检查以下示例
1- ImageView正在从storyBoard中获取 使用代码
添加了2- myImage现在,Storyboard最初植入了序列化和反序列化机制 因此,imageView是从故事板添加的属性,因此不需要编码器
import UIKit
class ImageVC: UIViewController {
@ IBOutlet var imageView: UIImageView!
// set up images
let picture1 = UIImage(named: "cc")
//ImageView programmaticlly
var myImage = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// set image on screen
imageView.image = picture1
//setting properties and frame
myImage.image = picture1
myImage.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
self.view.addSubview(myImage)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(count(_:)))
self.imageView.addGestureRecognizer(tapRecognizer)
self.imageView.isUserInteractionEnabled = true
}
// set up gesture recognizer
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//adding gesture
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(count(_:)))
self.myImage.addGestureRecognizer(tapRecognizer)
self.myImage.isUserInteractionEnabled = true
}
@objc func count(_ gestureRecognizer: UIGestureRecognizer) {
print("tapped the picture")
}
}
好像你在Algo上面实现它不会崩溃,因为从故事板添加的For Properties可以直接在didload或willAppear中访问,因为我们永远不会从superview中删除它们所以间接地将它序列化
第二种情况 - myImage - 以编程方式添加也可以从superView中删除并再次添加因此可以序列化意味着需要
答案 2 :(得分:0)
var image: UIImage?
@IBOutlet weak var imageChoisie: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageChoisie.image = image // from Segue
// Click on Image
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(count(_:)))
self.imageChoisie.addGestureRecognizer(tapRecognizer)
self.imageChoisie.isUserInteractionEnabled = true
}
@objc func count(_ gestureRecognizer: UIGestureRecognizer) {
//print("tapped the picture")
dismiss(animated: true, completion: nil) // back previous screen
}