How to animate changes in UIButton content alignment - ios

Several of UIButton's properties are not inherently animatable. One of these properties is the contentHorizontalAlignment property. After searching SO with no luck, I came up with the answer below to solve the "not-animatable" issue.

You should animate layoutIfNeeded after change alignment value:
Objective C:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
Swift3:
button.contentHorizontalAlignment = .left
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})

This method first animates UIButton's titleLabel frame manually, then sets the content alignment and content edge insets to preserve UIButton default behavior.
CGRect frame = button.titleLabel.frame;
frame.origin.x = 10;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
button.titleLabel.frame = frame;
} completion:^(BOOL finished) {
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}];

Related

how to change position of button with Animation

change position when it is pressed.
UIButton * button = sender;
CGPoint position = button.frame.origin;
[UIButton setAnimationDuration:1.0];
CGSize size = button.frame.size;
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height);
Try this:
[UIView animateWithDuration:1.0 animations:^{
btn.frame = CGRectOffset(btn.frame, 0, 20);
}];
For animating the position of UIButton with duration 1.0 seconds
CGPoint position = button.frame.origin;
CGSize size = button.frame.size;
-(IBAction)btnClick:(id)sender {
[UIView animateWithDuration:1.0 animations:^{
button.frame = CGRectMake(position.x+80,position.y,size.width,size.height);
}];
}
Try this :
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // initial frame
[UIView animateWithDuration:2 animations:^{
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
you can use Animation block of UIView class ...
whatever you change the frame inside this block , the canvas fill the frame in between
Set the Start frame
button.frame= yourInitialFrame;
[UIView animateWithDuration:1.0 animations:^{
// set the End Frame here
button.frame = CGRectMake(position.x +
80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
also see this see this for more help
[UIView animateWithDuration:1.0 animations:^{
// set the End Frame here
button.frame = CGRectMake(position.x +
80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
Try this:
-(IBAction)ButtonClick:(id)sender
{
UIButton *aBtnRef = sender;
[UIView animateWithDuration:0.5 animations:^{
aBtnRef.frame = CGRectMake(aBtnRef.frame.origin.x + 50,aBtnRef.frame.origin.y, aBtnRef.frame.size.width, aBtnRef.frame.size.height);
} completion:^(BOOL finished) {
}];
}
For More Info :
Animation : How to make UIbutton move from right to left when view is loaded?
Moving UIButton position, animated
[UIView animateWithDuration:0.2 animations:^{
CGRect rect = button.frame;
rect.origin.x = rect.origin.x + 80;
button.frame = rect;
} completion:^(BOOL finished) {
}];

Change UIScrollView contentSize with animation

Is it possible to change a UIScrollView's contentSize with an animation so the size decreases / increases on a certain period, something like
UIView.animateWithDuration(0.3, animations: {
scrollView.contentSize.width -= 100.0
}
Doesn't work. I don't know if there's a way to do that ?
Try the transitionWithView method:
float newHeight = scroller.contentSize.height - someChangeFloat;
[UIView transitionWithView:scroller
duration:0.3f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
scroller.contentSize = CGSizeMake(0, newHeight);
}
completion:^(BOOL finished){
}];

UIView animateWithDuration not setting frame size, when performed the first time

I have an instance of [UIView animateWithDuration: delay: options: animations: completion:] within a scrollViewDidEndDecelerating method that resizes a UIView to be a smaller height if the contentOffset is greater than 0.
For some reason, the frame doesn't stay the way it should on the second scroll. Meaning that despite the resize and the visible change in the frame, the frame jumps to it's larger height and reanimates back to the smaller height. This only seems to happen the second time scrolling, but it retains the desired height henceforth, throughout the lifecycle of the app.
I have no frame changes done other than in my instances of [UIView animateWithDuration: delay: options: animations: completion:], so why does this frame not stay the way it should at that time?
I like to point out that the scrollView is actually a horizontal collectionView, here is the code:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat offset = self.collectionView.contentOffset.x;
if(offset == 0){
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headingView.frame = CGRectMake(0, 0, 320, 95);
} completion:^(BOOL finished){
}];
}else if(self.headingView.frame.size.height != 60){
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headingView.frame = CGRectMake(0, 0, 320, 60);
} completion:^(BOOL finished){
}];
}
}

Reducing UIButton size with constant centre

I am trying to animate the reduction of size and width of a UIButton in this way:
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveEaseInOut
animations:^{
[button setFrame:CGRectMake(button.frame.origin.x, button.frame.origin.y, 0, 0)];
[button setCenter:CGPointMake(button.frame.origin.x, button.frame.origin.y)];
}
completion:^(BOOL finished){
...
The button is reducing in size, but the centre is not constant. I would like it to close on itself, if you know what I mean, like when you delete an IOS app
Use CGRectInset on the current frame to produce a new frame which is smaller (or larger) than the original while maintaining the center point.
[button setFrame:CGRectInset(button.frame, button.frame.size.width * 0.5, button.frame.size.height * 0.5)];
Use the original center:
CGPoint center = button.center;
button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y, 0, 0);
button.center = center;
[button setCenter:CGPointMake(button.frame.origin.x, button.frame.origin.y)];
Comment this one out and try. If it doesn't work get the centre point before animation and set the point exclusively as mentioned rmaddy.
You can also try :
[button setFrame:CGRectMake(button.frame.origin.x+button.frame.size.width/2, button.frame.origin.y+button.frame.size.height/2, 0, 0)];
In your code, it would be:
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveEaseInOut
animations:^{
[button setFrame:CGRectMake(button.frame.origin.x+button.frame.size.width/2, button.frame.origin.y+button.frame.size.height/2, 0, 0)];
}
completion:^(BOOL finished){
...
I've tested this and it "shrinks" the button maintaining the center throughout the animation.
[UIView animateWithDuration:0.5 delay:0.0 options:0
animations:^{
button.transform = CGAffineTransformMakeScale(0, 0);
} completion:^(BOOL finished) {
// ... remove button from super view?
}];

Resizing UIView with UIToolbar do not resize simultaneously

I have created a UIView with a UIToolbar both programmatically and with IB. I try to resize the UIView but the tool bar resizes first and make it obvious that there are 2 views. Can anyone tell me how to resize both view simultaneously so that it give the appearance that the UIView and toolbar are one? I have set all the appropriate masks. The code below is called on the UIView from the UIView class.
Thanks!!!
[UIView animateWithDuration: .3 animations:^ {
CGRect frame = self.frame;
frame.origin.x = 0;
frame.origin.y = 20;
frame.size.height = 900;
frame.size.width = 768;
self.frame = frame;
}];
Animate both at the same time:
[UIView animateWithDuration: .3 animations:^ {
toolbar.frame = CGRectMake(0.0, 20.0, 900.0, 768.0);
self.frame = CGRectMake(0.0, 20.0, 900.0, 768.0);
}];
Or use some NSLayoutConstraint to achieve equal frames.

Resources