This really does not work when I try to do this, I had a related question in which the layoutIfNeeded () method was placed inside the animation and did a smooth viewing of the animation (the button for moving to the target using the restrictions, no reaction? ). But in this case, it does not work with backgroundColor. If someone knows the answer, I will be interested to know.
But if you need a solution right now, you can create a UIView (programmatically or through a storyboard) that is used only as a container. Then you add 2 views inside: one on top and one below, with the same frame as the container. And you only change the top view alpha, which allows the user to see the view behind:
class MyView : UIView { var top : UIView! override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = UIColor.blueColor() top = UIView(frame: CGRectMake(0,0, self.frame.width, self.frame.height)) top.backgroundColor = UIColor.yellowColor() self.addSubview(top) } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let sub = UIView(frame: CGRectMake(0,0, self.frame.width, self.frame.height)) sub.backgroundColor = UIColor.purpleColor() self.sendSubviewToBack(sub) UIView.animateWithDuration(1, animations: { () -> Void in self.top.alpha = 0 }) { (success) -> Void in println("anim finished") } } }
source share