Reputation: 8503
I'm animating a custom UIView to expand, as shown in the gif below (the red view). When clicking the open/close-button, I want the left icon to stay in its superview's center, vertically. As shown below, as soon as I click the button, the icon will pop to where it should be after the animation is done. The same goes for the button, but it's not as noticeable as it gets temporarily hidden from clicking it.
I was also hoping for the UILabel
in between to animate its size, rather than popping to minimum height as soon as you click 'Close'.
In Storyboard I have set the icon and button to align center with the superview, which to me seems to be the correct way to achieve what I want. I would assume that when I animate the superview, that the subviews would stay center during the animation, but instead they instantly move to the point that WILL be correct, but after the animation is completed.
My animation code:
UIView.animateWithDuration(0.4, animations: { () -> Void in
var rect = self.frame; //The current frame, to change
let oldHeight = self.frame.size.height as CGFloat
let newSize = self.sizeForBanner() //Get the CGSize for the big banner.
if(self.isBig) //Animate to big size
{
//Put the new height and origin for the large version of the view
rect.size.height = newSize.height;
rect.origin.y -= (rect.size.height - oldHeight)
}else{ //Animate to small size
//Put the new height and origin for the small version of the view
rect.size.height = self.minimalHeight;
rect.origin.y += oldHeight-rect.size.height
}
//Set the new variables
self.frame = rect;
}) { (Bool) -> Void in
//Completion
}
How /what do I change to make the icon and the button to stay centered when the superview animates?..
Upvotes: 2
Views: 1439
Reputation: 1371
You need to have Clip Subviews
enabled. This is a value that is disabled by default for UIViews.
If you created the view through Interface Builder go to Attributes Inspector
and check the Clip Subviews
checkbox:
If you created the view through code then use:
view.clipsToBounds = true
Upvotes: 2