I'm trying to run an example to test this function. I have a label an a button in the storyBoard and I have referenced the bottom constraint of the label in the view controller. When I use the button I want the label to move with an animation but it moves without it. Here's the code of the button:
- (IBAction)sd:(id)sender {
[UIView animateWithDuration:0.5f animations:^{
self.constraint.constant += 50;
}];
}
I know how to use it in swift but I'm having problems in objective c and I know it will be just for a little mistake... Any help?
This is not the right way of animating a UI component constrained with Autolayout. You should first update the constraint, then call layoutIfNeeded within the animation block:
self.constraint.constant += 50;
[UIView animateWithDuration:0.5f animations:^{
[self.view layoutIfNeeded];
}];
Use this
self.constraint.constant += 50;
[UIView animateWithDuration:0.5f
animations:^{
[self.view layoutIfNeeded];
}];
Try this setNeedsLayout helps in some cases.
self.constraint.constant += 50;
[UIView animateWithDuration:0.5f
animations:^{
[self.view setNeedsLayout];
}
completion:^(BOOL finished) {
}];
Related
I want to animate the center of a UIView, and I did this in viewDidLoad:
_test.center = CGPointMake(100.0,100.0);
[UIView animateWithDuration:10.0
delay:5
options:UIViewAnimationOptionCurveLinear
animations:^{_test.center = CGPointMake(100.0,-100.0);}
completion:^(BOOL finished) {
}];
But when I run the code, the center of test view is already at 100,-100. Why it doesn't animate?
Move the code from your viewDidLoad to your viewDidAppear. In viewDidLoad, the UIView has been loaded, but it is not visually present yet.
I guess you are using auto constraints which prevent animating views the way you are doing.
In order to fix this issue try these steps:
First:
Move your code to - (void)viewDidLayoutSubviews.
Second:
Do these steps before and after doing any animation to your view:
- (void)viewDidLayoutSubviews{
_test.translatesAutoresizingMaskIntoConstraints = YES;
_test.center = CGPointMake(100.0,100.0);
[UIView animateWithDuration:10.0
delay:5
options:UIViewAnimationOptionCurveLinear
animations:^{_test.center = CGPointMake(100.0,-100.0);}
completion:^(BOOL finished) {
}];
[_test updateConstraints];
}
I have an animation of a contentView of a cell wish have UIPanGestureRecognizer.
the UIPanGestureRecognizer works fine and detect touches but while the animation is happening it doesn't detect touches until it finish the animation.
is there a workaround for this.
this is the animation block
[self.myContentView layoutIfNeeded];
self.contentViewLeftConstraint.constant = -50;
self.contentViewRightConstraint.constant = 50;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.myContentView layoutIfNeeded];
} completion:completion];
Thanks.
If you want to allow user interaction during the animation, you must set the option to allow it:
UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseOut |
UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:duration delay:0 options:options animations:^{
[self.myContentView layoutIfNeeded];
} completion:completion];
i am trying to change tableview frame by using autolayout in animationWithDuration block. Like That;
[UIView animateWithDuration:0.3f animations:^{
weekdayOffersVerticalConstraint.constant = 80;
[headerView layoutIfNeeded];
[weekdayTableView layoutIfNeeded];
self.isTableViewSmall = YES;
} completion:^(BOOL finished) {
}];
my tableview's frames are changing with this code block, but when i scroll to down, cells coming from left side to right side, i thought it is about with [weekdayTableView layoutIfNeeded]; layoutIfNeeded effect to all subviews. How can i obstruct to effect to my tableview's cells?
Sorry for my bad english, Thank you very much for your answers and advices.
Call layoutIfNeeded before you begin the animation to update the constraints:
[headerView layoutIfNeeded];
[weekdayTableView layoutIfNeeded];
[UIView animateWithDuration:0.3f animations:^{
weekdayOffersVerticalConstraint.constant = 80;
[headerView layoutIfNeeded];
[weekdayTableView layoutIfNeeded];
self.isTableViewSmall = YES;
} completion:^(BOOL finished) {
//
}];
You probably also don't want to set that BOOL property inside the animation block unless you have a method that overrides it intentionally during the animation.
I have toolbar on my view and I want to move it with animation, but the animation does not work, the toolbar move quickly to the new position. I use it:
[UIView animateWithDuration:2.0f animations:^{
[self.view layoutIfNeeded];
self.toolbarBottomLayoutConstraint.constant = editing ? 0 : -44;
}];
Does anybody know why?
I've tried just changing animation block sentences and it worked for me:
[UIView animateWithDuration:2.0 animations:^{
self.bottomMargin.constant = editing ? 0 : -44;
[self.view layoutIfNeeded];
}];
Why don't you do something like this?
[UIView animateWithDuration:2.0f
animations:^{
yourView.center = CGPointMake(yourView.frame.size.width/2, -44);
} completion:^(BOOL finished) {}];
Hi I have piece of code which only one line is not working...
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
[self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working
[self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
[self.currentLabel removeFromSuperview];
self.currentLabel = label;
}];
I'm running out of ideas what is wrong...
What I find out is: if I turn off "Use Auto Layout", then it magically working, so this issue is connected with auto layout.
After a search, I find this: http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was
Add constraint outlet mapping for your view, then instead of using setFrame, updating constraints will do the trick!
Below is my final implementation (_topConstraint is the top vertical space constraint of table view):
- (IBAction)switchButtonTouched:(id)sender
{
if (_topConstraint.constant <= 0)
_topConstraint.constant = _buttonView.frame.size.height;
else
_topConstraint.constant = 0;
[_tableView setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5f animations:^(void){
[_tableView layoutIfNeeded];
} completion:^(BOOL finished){
}];
}
Frame must be a CGRect with 4 parameters: (xPosition,yPosition,width,height). Use CGRectMake for set frame
self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height)
You set 0.3 sec the animation duration, and when it s end you remove it from the view. This is too short. set the duration for example 2, and you will see that your label is moving .
Sometimes you should set frames for animating views also after animation, for example in completion block, try something like that
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
[self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working
[self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
self.frame = currentLabelFrame;
label.frame = label.frame;
[self.currentLabel removeFromSuperview];
self.currentLabel = label;
}];
If your issue is neither Autolayout related, or the others listed here, there is also another possible problem that turned out to be my issue. I'm simultaneously running a UIView transition (transitionFromView:toView:) on views that take up the same screen space (Different superviews, not related, other than positioning in the superview of the overarching view controller).
My code looked like this:
[UIView transitionFromView:self.someView
toView:self.someOtherView
duration:0.6f
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionShowHideTransitionViews
completion:NULL];
[UIView animateWithDuration:0.3
delay:0.0 options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.someThirdView setFrame:someFrame]; //not working
[self.someThirdView setAlpha:1.0f]; //working
} completion:nil];
The frame changed, but it popped to the new frame rather than animating. I'm guessing this is an internal iOS issue. As soon as I removed the "transitionFromView:..." call, the frame animation worked fine.
My solution for now has been to move the second animation into the completion block of the transitionFromView. It's not perfect, as the two animations should have lined up, but it is a passable solution for now.