捏缩放在CollectionView单元格中的UIImageView

时间:2017-12-31 12:34:08

标签: ios swift uicollectionviewcell

我尝试添加IBAction来将图像捏合并缩放到自定义单元格类,但是故事板会出现错误,即操作无法针对重复内容。 这是自定义单元格类:

class PhotoCell: UICollectionViewCell {

// Outlets
@IBOutlet weak var photoImgV: UIImageView!
@IBOutlet weak var captionTV: UITextView!
@IBOutlet weak var captionBtn: UIButton!
@IBOutlet weak var captionStackView: UIStackView!
@IBOutlet weak var likeCountLabel: UILabel!
@IBOutlet weak var comntCountLabel: UILabel!
@IBOutlet weak var pinch: UIPinchGestureRecognizer!

// Actions
@IBAction func pinch(sender:UIPinchGestureRecognizer) {
    if sender.state == .began || sender.state == .changed {
        let currentScale = self.photoImgV.frame.size.width / self.photoImgV.bounds.size.width
        let newScale = currentScale*sender.scale
        let transform = CGAffineTransform(scaleX: newScale, y: newScale)
        self.photoImgV.transform = transform
        sender.scale = 1
    }
}

@IBAction func showCaption(){
    UIView.animate(withDuration: 0.5) {
        if self.captionStackView.arrangedSubviews[1].isHidden {
            self.captionStackView.arrangedSubviews[1].isHidden = false
        }else{
            self.captionStackView.arrangedSubviews[1].isHidden = true
        }
    }
}

它允许我将IBAction连接到按钮以在同一单元格中显示/隐藏字幕,那么为什么不允许捏?如何在单元格中捏合和缩放图像?

1 个答案:

答案 0 :(得分:0)

我通过从storyboard中删除捏合手势并在awakeFromNib()方法中创建它来解决它:

    override func awakeFromNib() {
    super.awakeFromNib()
    let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.pinch(sender:)))
     photoImgV.addGestureRecognizer(pinch)
}

其他一切都是一样的。