ios - 我的 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
类 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()
}
}
推荐阅读
- bash - 当脚本在 crontab 与手动命令中运行时,ssh 命令不起作用
- python-3.x - CMD 和 PowerShell 中的 AWS CLI
- sql-server - T-SQL:如何避免对这个 XML 进行字符串操作?
- java - 如何在android中重复视图动画变量次数?
- javascript - 如何在 Mongodb 上获取每天的数据平均值?
- python - 在 iPython 中使用 style.format 打印 pandas df
- java - 发送@RequestPart 列表
由于 Post Request Body 给出 Content-Type Error - firebase - Firebase/Firestore 数据不是函数
- excel - 如何使数字显示为 hh:mm(持续时间,而不是一天中的时间)
- node.js - MongoDB:将数据正确保存到模型