如何解决我的 UITapGestureRecognizer 影响 TableViewCell 中的所有元素
我正在尝试做我的第一个应用程序,我需要添加 UITapGestureRecognizer 来缩放 TableViewCell 中的照片。
它可以工作,但不正确 - 我的手势不仅可以缩放 ViewCell 中的照片,还可以缩放 ViewCell 中的所有元素。我该如何解决这个问题?
我所有的 TableViewCell 代码:
import UIKit
class PhotosCollectionViewCell: UICollectionViewCell {
static let identifier = "PhotosCollectionViewCell"
@IBOutlet weak var mainPhoto: UIImageView!
@IBOutlet weak var mainLabel: UILabel!
var isInScale: Bool = false
func configure(photo: PhotosInProfile) {
mainPhoto.image = UIImage(named: photo.mainPhoto)
mainLabel.text = "Date: \(photo.date)"
}
override func awakeFromNib() {
super.awakeFromNib()
let doubleTap = UITapGestureRecognizer(target: self,action: #selector(imageDoubleTapped))
doubleTap.numberOfTapsRequired = 2
mainPhoto.isUserInteractionEnabled = true
mainPhoto.addGestureRecognizer(doubleTap)
}
@objc func imageDoubleTapped() {
if isInScale {
self.transform = .identity
} else {
self.transform = CGAffineTransform(scaleX: 1.5,y: 1.5)
}
isInScale.toggle()
}
}
解决方法
导入 UIKit
class PhotosCollectionViewCell: UICollectionViewCell {
static let identifier = "PhotosCollectionViewCell"
@IBOutlet weak var mainPhoto: UIImageView!
@IBOutlet weak var mainLabel: UILabel!
var isInScale: Bool = false
func configure(photo: PhotosInProfile) {
mainPhoto.image = UIImage(named: photo.mainPhoto)
mainLabel.text = "Date: \(photo.date)"
}
override func awakeFromNib() {
super.awakeFromNib()
let doubleTap = UITapGestureRecognizer(target: self,action: #selector(imageDoubleTapped))
doubleTap.numberOfTapsRequired = 2
mainPhoto.isUserInteractionEnabled = true
mainPhoto.addGestureRecognizer(doubleTap)
}
@objc func imageDoubleTapped() {
if isInScale {
mainPhoto.transform = .identity
} else {
mainPhoto.transform = CGAffineTransform(scaleX: 1.5,y: 1.5)
}
isInScale.toggle()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。