ios - 动画时将 UITableViewCell 子 UIImageView 置于前面

标签 ios swift uitableview uiimageview

我有一个 UITableViewCell。 在这个 tableviewcell 中,我有一个 UIImageView。

我想在按下图像时将 CGAffineTransform 应用到 UIImageView。

问题:单元格的其他项目在我的图像之上,我希望它们在动画出现时位于后面。

我试过这样的事情:

//In the UITableViewCell

override func didMoveToWindow() {
    super.didMoveToWindow()

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

但这不起作用。

我也试过:

photoView.layer.zLocation = 10

但这也不起作用。

对于那些想知道的人,转换代码如下所示:

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

有什么想法吗?

最佳答案

如果我没记错的话,这就是你想要的:

“我想在按下图像时将 CGAffineTransform 应用于 UIImageView” - 在其他单元格的 subview 之上 - 意味着您应该为 imageView 设置 UITapGestureRecognizer :

您的 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
            }
        }

    }
}

输出应该是这样的:

  • 首次发布:

enter image description here

  • 点击手机:

enter image description here

  • 重新点击它:

enter image description here

希望对您有所帮助。

关于ios - 动画时将 UITableViewCell 子 UIImageView 置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39889229/

相关文章:

ios - 获取权限后如何刷新权限

ios - 在 iOS Swift 中获取所有 UIViewControllers 列表

regex - 如何仅从此字符串获取链接?

ios - 停止对 firebase 的查询?

ios - 延迟 UITableView 隐藏 UITableViewCells

iphone - 如何在 iPhone 上本地化 "Timer"

iphone - 从rails下载到iphone压缩文件缺少部分

swift - FirebaseUI 不显示任何输入字段或按钮

iphone - 如何禁用 UITableView 的单个单元格的选择?

ios - NSSortDescriptor 按最后一条消息时间戳对对话进行排序