简体   繁体   English

动画时将UITableViewCell子UIImageView放在前面

[英]Bringing a UITableViewCell child UIImageView to front while animating

I have a UITableViewCell. 我有一个UITableViewCell。 In this tableviewcell, I have an UIImageView. 在这个tableviewcell中,我有一个UIImageView。

I want apply a CGAffineTransform to the UIImageView when I press the image. 我想在按下图像时将CGAffineTransform应用于UIImageView。

The problem: Other items of the cell are on top of my image and I want them to be in the back when the animation occurs. 问题:单元格的其他项目在我的图像上方,并且我希望它们在动画发生时位于背面。

I tried things like: 我尝试过类似的事情:

//In the UITableViewCell

override func didMoveToWindow() {
    super.didMoveToWindow()

    superview?.superview?.bringSubview(toFront: photoView)
  }

but that doesn't work. 但这不起作用。

I also tried to do: 我也尝试做:

photoView.layer.zLocation = 10

but that doesn't work either. 但这也不起作用。

For those wondering, the transform code looks like: 对于那些想知道的人,转换代码如下:

let scaleTransform = CGAffineTransform(scaleX: scale, y: scale)
photoView.transform = scaleTransform

Any idea? 任何想法?

If I'm not mistaking this is what you want: 如果我没有记错,这就是您想要的:

"I want apply a CGAffineTransform to the UIImageView when I press the image" -on top of other cell's subviews- means that you should set a UITapGestureRecognizer for the imageView: “我想在按下图像时将CGAffineTransform应用于UIImageView” -在其他单元格的子视图之上-意味着您应该为UITapGestureRecognizer设置UITapGestureRecognizer

Your UITableViewCell should be similar to: 您的UITableViewCell应该类似于:

// your custom cell, in my case I'm calling it "TableViewCell"
class TableViewCell: UITableViewCell {
    @IBOutlet weak var imgView: UIImageView!
    // your other IBOutlets...

    //...

    // this flag is useful when you want to check if the imageView is tapped, you should return it to its normal transform -for example-
    private var isImageViewMovedToFront = false

    override func didMoveToWindow() {

        // adding the tap gesture
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(TableViewCell.imgViewTapped))
        imgView.isUserInteractionEnabled = true
        imgView.addGestureRecognizer(tapGesture)

    }

    func imgViewTapped() {
        if isImageViewMovedToFront == false {
            isImageViewMovedToFront = true

            contentView.bringSubview(toFront: imgView)
            UIView.animate(withDuration: 0.25) {
                let scaleTransform = CGAffineTransform(scaleX: 1.2, y: 1.2)
                self.imgView.transform = scaleTransform
            }
        } else {
            isImageViewMovedToFront = false

            contentView.sendSubview(toBack: imgView)
            UIView.animate(withDuration: 0.25) {
                let scaleTransform = CGAffineTransform(scaleX: 1.0, y: 1.0)
                self.imgView.transform = scaleTransform
            }
        }

    }
}

The output should looks like: 输出应如下所示:

  • first launch: 首次启动:

在此处输入图片说明

  • tapping on the cell: 点击单元格:

在此处输入图片说明

  • retapping on it: 重拍:

在此处输入图片说明

Hope that helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM