ios - CGAffineTransform 在其他 View 上

标签 ios swift cgaffinetransform

我的 View 中有九个 subview 。 (topLeft, top, topRight, left, center, right, bottomLeft, bottom, bottomRight)。当我在中心 subview 中使用 CGAffineTransform 执行 animation 时。

这是一个棘手的问题。中心顶部的 subview 将被覆盖;其他的不会被覆盖。为什么?

如何覆盖动画中的所有 subview ?

origin problem expected

    override func viewDidLoad() {
        super.viewDidLoad()

        let top = UIView()
        top.backgroundColor = .red
        top.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height/2, width: 20, height: 20)
        top.layer.masksToBounds = true
        view.addSubview(top)

        let topLeft = UIView()
        topLeft.backgroundColor = .orange
        topLeft.frame = CGRect(x: self.view.frame.width/2 - 20, y: self.view.frame.height/2, width: 20, height: 20)
        topLeft.layer.masksToBounds = true
        view.addSubview(topLeft)

        let topRight = UIView()
        topRight.backgroundColor = .gray
        topRight.frame = CGRect(x: self.view.frame.width/2 + 20, y: self.view.frame.height/2, width: 20, height: 20)
        topRight.layer.masksToBounds = true
        view.addSubview(topRight)

        let center = UIView()
        center.backgroundColor = .black
        center.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height/2 + 20, width: 20, height: 20)
        view.addSubview(center)

        let left = UIView()
        left.backgroundColor = .cyan
        left.frame = CGRect(x: self.view.frame.width/2 - 20, y: self.view.frame.height/2 + 20, width: 20, height: 20)
        view.addSubview(left)

        let right = UIView()
        right.backgroundColor = .brown
        right.frame = CGRect(x: self.view.frame.width/2 + 20, y: self.view.frame.height/2 + 20 , width: 20, height: 20)
        view.addSubview(right)

        let bottom = UIView()
        bottom.backgroundColor = .yellow
        bottom.frame = CGRect(x: self.view.frame.width/2 , y: self.view.frame.height/2 + 20 + 20, width: 20, height: 20)
        view.addSubview(bottom)

        let bottomLeft = UIView()
        bottomLeft.backgroundColor = .magenta
        bottomLeft.frame = CGRect(x: self.view.frame.width/2 - 20, y: self.view.frame.height/2 + 20 + 20, width: 20, height: 20)
        view.addSubview(bottomLeft)


        let bottomRight = UIView()
        bottomRight.backgroundColor = .green
        bottomRight.frame = CGRect(x: self.view.frame.width/2 + 20, y: self.view.frame.height/2 + 20 + 20, width: 20, height: 20)
        view.addSubview(bottomRight)

        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))

    }

    func handleTap(gesture: UITapGestureRecognizer) {
        let location = gesture.location(in: view)
        for subview in view.subviews {
            if subview.frame.contains(location) {
                let scaleUp = CGAffineTransform(scaleX: 3, y: 3)

                UIView.animate(withDuration: 0.5, animations: {
                    subview.transform = scaleUp
                }) { (success: Bool) in
                //subview.transform = .identity
                }
            }
        }
    }

最佳答案

bringSubviewToFront 在应用转换之前。它将位于所有其他 subview 之上并覆盖它们。

关于ios - CGAffineTransform 在其他 View 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43512812/

相关文章:

ios4 - iPhone 帮助重置动画 CGAffineTransform?

ios - 在 ios 中使用好友系统解析 swift

ios - 无法在委托(delegate)回调方法中更新文本字段

ios - 基于 IOT 的应用程序向 AppStore 提交流程

swift - ubuntu Swift 中日历功能的替代方案?

swift - 类型 '[Class]?' 没有成员 '[Variable]'

ios - Xcode 在我删除派生数据文件夹后继续重新制作它

ios - 如何在 iOS Swift 中显示库中的实时照片视频并显示在 Collection-View 中?

ios - super View 转换后如何找到 UIView 的中心?

opencv - 为什么我不能使用opencv仿射变换来翻转图像?