There are nine subviews in my view. (topLeft, top, topRight, left, center, right, bottomLeft, bottom, bottomRight). When I do the animation
with CGAffineTransform
in the center subview.
This is a tricky question. Subviews top to the center will be covered; the others will not be covered. Why?
How can I do to cover all subviews in my 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
}
}
}
}