UIView animation on iPad doesn't take into account orientation - ios

I'm trying to slide my main view controller view to the right when the user taps a button. I use this code:
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^(void){ self.view.left = 300; }
completion:nil];
The result is that view.frame.origin.x does not take into account device orientation. If the screen is landscape left, then setting x = 300 does not move the view to the right, it moves it down. I could do a big switch statement to change the relevant parameter depending on orientation but I feel like I've done this before without having to resort to such shenanigans. Am I missing something obvious?
I've tried with no options, no difference.
I'm using a category on UIView so that I don't have to type out the entire CGRectMake explicitly. The results are the same if I do it with the full CGRectMake statement.

I had the same issue. Animating the view's bounds instead of the view's frame (I am assuming this is what the category is doing) worked for me.

Related

prevent to view appear with animmation

I have a problem that my custom navigation bar appears for the first time with animation. I think it happens because I use auto-layout and it animates it self into the state of landscape or portrait. But I want to have a functionality, that after first time I enter the screen everything is still, and where it belongs. and after that if I turn the screen or if I do something all the animations appears like now.
Is there a good think to omit the first animations when the view creates itself?
The animations are : labels floating from left to right. and 1 label appears as from 0px width and height it scales into 100% width and height
code:
[self.navigationController.navigationBar addSubview:self.topBar];
self.topbar is UIView. I have added it to block [UIView performWithoutAnimation:^{ }]; but it does not helps, everytime the view appears for the first time I have my labels floating from left to right.
The answer is that I used some methods [self layoutIfNeeded]; After setting the constraint. The solution is to have a method set like this:
- (void) yourAnimation:(BOOL)animated{
if(animated){
[UIView animateWithDuration:0.3 animations:^{
//your animated code
//[self layoutIfNeeded];
}];
}else{
[UIView performWithoutAnimation:^{
//your stuff without animation
}];
}
}

UIView animateWithDuration not reacting as I expected

I'm a little confused by UIView animateWithDuration.
I have a label placed at (224,93,152,39), centred horizontally in my storyboard. When I run viewDidLoad, I want it to slide up from centre vertically and horizontally to the position I've placed in my storyboard. So intuitively, I did this:
[UIView animateWithDuration:1.0 animations:^{
self.titleLabel.frame = CGRectMake(self.titleLabel.frame.origin.x,self.view.center.y, 0, 0);
}];
This actually did the opposite for me. The label slides from above the status bar to the desired position (224,93,152,39). I want it to start from centre of the viewController to the desired position (224,93,152,39).
I think I'm not getting something essential of the UIView animations. Would appreciate it if someone can point it out for me.
May be you can try by place the above code to viewDidAppear method
The frame you set in the animations block is the ending frame. Set the starting frame right before you call animateWithDuration if needed.

How to translate an entire UIView and retain gesture recognition?

I have a UIView "MainView" that initially appears as follows:
The gradient bar is part of MainView, the whitespace beneath is part of a Container View subview.
When the search button in top-right is tapped, I animate a searchBar from offscreen to be visible in the view:
I manage to do this by the following code:
CGRect currentViewFrame = self.view.bounds;
currentViewFrame.origin.y += searchViewHeight;
[UIView animateWithDuration:0.4
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.view.frame = currentViewFrame;
}
completion:^(BOOL finished)
{
}];
Visually, the result of this animation is perfect. The entire view shifts, and the searchBar is now on screen. However, the searchBar does not respond to interaction. Correct me if I'm wrong, but I expect this is because the MainView's frame no longer includes the screen area that the searchBar now occupies, so its effectively a gesture deadzone.
So this makes me think that instead of lazily animating the entire MainView down to accomodate the searchBar, I must instead individually translate all subviews of MainView one at a time. In this simple situation, that would not be a big problem, but I can envision a circumstance with tens of subviews making that completely unrealistic.
What is the best method to accomplish what I am trying to do? Is there a secret to animating entire views/subviews without having gesture deadzones? Thanks in advance!!

Animate exit of UIView

I want to make a UIView animate when it's being closed. I tried reading the following:
http://felipe.sabino.me/ios/2012/05/10/ios-uiview-transition-effects/
iPhone UIView Animation Best Practice
iOS UIView Animation CATransform3DMakeRotation confusion
However, I'd like to make it transition from the side of the screen, as per the image in the Google Chrome app.
Is there another animation that is set for this? I was not able to find it... I'm assuming it has to do with animateWithDuration or a CATransform...can somebody point me in the right direction for this?
[EDIT]
I used the below post for an answer as well as this post:
Setting a rotation transformation to a UIView or its layer doesn't seem to work?
I was able to add multiple animations as per below:
[UIView animateWithDuration: .2
delay: 0
options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations:^{self.view.center = CGPointMake(self.view.frame.origin.x * 3, self.view.frame.origin.y * 2), self.view.transform = CGAffineTransformMakeRotation(M_PI_4/2);}
completion:nil];
Previously I was not aware you can add multiple animations so easily. That adds rotation as well as the linear movement together.
Animate your view so it moves offscreen/shrinks/expands/fades, then do the actual removal when the animation ends.
You can do this by altering the properties of the view (position/size/offset) between a beginAnimations/commitAnimations block. UIKit will then animate these properties over the time specified.
E.g something like;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
view.transform =
CGAffineTransformMakeTranslation(
view.frame.origin.x,
480.0f + (view.frame.size.height/2) // move the whole view offscreen
);
background.alpha = 0; // also fade to transparent
[UIView commitAnimations];
In the animation end notification you can then remove the view.
I've never run Chrome on iOS, so I have to try to guess what your screenshot is showing.
Does the animation go off the screen while shrinking and turning to one side?
And do you mean you want to animate a UIViewController, or a UIView? Are you closing a view controller?
If it's a view controller, how are you managing your view controllers? Are you using a navigation controller, or are you presenting a set of modal view controllers, or some other method?

How I could animate changing UIViewControllers?

I have a UIViewController based app and I want to changes data on view with animation (handle touchesbegan, touchesmove, touchesended). All data I save in a base). For example, I have main screen, and when (touches ended) handles, I want to show other view.
Whatever i will be to do, screens could be rotate (to right or to left, if position changes more then 1/2 of width View, but if labels count number more then count of rows in my table, I want to set state at image 2.
Could you see right way to do animations? For example, I could to create my own class or use app delegate, but I not sure from what I must to start.
Use UIView's transitionsWithView method:
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView]; }
completion:NULL];
More details, including above example can be found here:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816
Tim

Resources