CGAffineTransform 在其他视图上
Posted
技术标签:
【中文标题】CGAffineTransform 在其他视图上【英文标题】:CGAffineTransform over other view 【发布时间】:2017-04-20 07:22:48 【问题描述】:在我看来有九个子视图。 (左上、上、右上、左、中、右、左下、下、右下)。当我在中心子视图中使用CGAffineTransform
执行animation
时。
这是一个棘手的问题。将覆盖中心顶部的子视图;其他的将不被覆盖。为什么?
如何才能覆盖动画中的所有子视图?
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
【问题讨论】:
【参考方案1】:bringSubviewToFront
在应用转换之前。它将高于所有其他子视图并覆盖它们。
【讨论】:
你是最棒的! 当然。发布后接受答案有时间限制:D @WeiJay 对不起-我的错。谢谢!以上是关于CGAffineTransform 在其他视图上的主要内容,如果未能解决你的问题,请参考以下文章
旋转超级视图后计算 CGAffineTransform 矩阵
有没有办法计算将视图从帧 A 转换到帧 B 所需的 CGAffineTransform?
CGAffineTransform 缩放和平移 - 动画前跳转