Disable Animation while in willAnimateRotationToInterfaceOrientation? - iphone

I have a scrollview that has a bunch of photo thumbnails on it just like in the photos app / image picker. When I rotate it, I want to update the scroll view in the middle of the rotation animation (in willAnimateRotationToInterfaceOrientation), but I just want it to update without animation (don't want the thumbnails to animate when they move to their new position). How can I accomplish this? I'm using setCenter to move the thumbnails when the rotation occurs. I've tried all of the other rotation methods but they don't get the job done because I need the update to occur while the rotation animation is occurring because that's when it looks the best.

Using the following works in the willAnimateRotationToInterfaceOrientation method:
[UIView setAnimationsEnabled:NO];
//insert what you want to not be animated here
[UIView setAnimationsEnabled:YES];

Related

iPhone How to get photo taken effect like in camera application?

I am talking about that effect when user presses button to take photo, it shrinks and moves to toolbar? How is this achieved in general?
You can create this by both scaling (applying a transform) and moving (animating the position) of the image.
I wrote about a similar animation (the Open in Background animation from Safari on iPhone) in this blog post. Not all of the code is necessary but some part of it will be useful for the animation you are trying to do.
You should
Calculate the scale factor to make the image the appropriate size
Calculate the path you want to animate the image along.
Animate a scale with the calculated scale factor
Animate a position animation along the path (using a CAKeyframeAnimation)
Since you are doing two animations at once you could benefit from using a CAAnimationGroup.
Since you are animating down to a toolbar which probably is another part of the view hierarchy than where the image is you may need to use method like
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
and
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
To translate coordinates between the different views.
so the shrink animation is achived by
[UIView beginAnimations:#"animationShrink" context:NULL];
[UIView setAnimationDuration:kSlideInAnimationDuration];
flipFlopContainer.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(shrinkAnimationFinished:)];
[UIView commitAnimations];
after this animation in shrinkAnimationFinished method you need to define a path to follow and change the position of your view accroding to that path.
See the following thread for that animation
Resize and move a UIView with Core Animation (CAKeyFrameAnimation)

Is there a way to change content offset via Core Animation on UIScrollView with reusing views?

EDIT: I got some progress, so I have simplified and narrowed the problem: I have created new question, which is more clearer and more understandable I hope
I have very large horizontally scrolling UIScrollView, which is reusing its subviews. When user scrolls left, the rightmost view is moved to the leftmost position and its content is updated. Similarly when user scrolls right, the leftmost view is moved to the rightmost position and its content is updated. Similarly like in UITableView. This works nicely using scrollviewDidScroll: delegate method.
Now I need to change offset programatically, but with custom animation behaviour (inertia and bounce back to the final position).
To prototype this, I use
[UIView animateWithDuration:contentOffsetCADuration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
[self setContentOffsetNoDelegateCalls:contentOffset animated:NO];
}
completion:^(BOOL finish){
dispatch_async(dispatch_get_main_queue(), ^{
[timer invalidate];
});
}];
where I animate content offset with disabled delegate callback, so that views are not messed during animation. But I need something to simultaneously reuse and reposition them.
My solution is to create timer and call scrollViewDidScroll: manually so that reusing works as usual, parallel to animation. But it simply does not work - nothing is reused and scrollview's content after animation is in the same state as before animation -> formerly visible pages are moved out of screen.
So the question narrows down how to move subviews and update their content during animation?
Or maybe there might be completely different way doing this ...
Check my answer over here.
Edit: lol, just noticed it's the same author
you may try to change scroll offset in a methode then you can call it from
[self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#>];
then make it stop when it reaches a specific value

Making an animating main menu

What would be the best way to make a main menu for a game? I would prefer it to be UIButtons. More specifically, what would be the best way of animating the buttons? For example, an animation on the view load of the buttons going in, then the buttons going out when a menu option is selected and then new buttons (the submenu) animated back in. I have never done any animating before so I would like to know the best way. Thanks
This type of animation is very easy using UIKit:
[UIView animateWithDuration:0.5 animations:^{
button1.frame = <new frame>;
button2.frame = <new frame>;
}];
This will animate the frame from the current frame, to the specified frame over the given duration (0.5 seconds). If the current frame is offscreen and the new frame is the final position you want then the buttons will move to their correct position over 0.5 seconds.
The following properties of UIView are animatable in this fashion:
frame
bounds
center
transform
alpha
backgroundColor
contentStretch
The Animations section of the View Programming Guide has more info.

I want to animate, in fact fade, "within" drawRect

Is there a way to make drawRect animate FROM THE PREVIOUS SCENE to the next one?
(Amazingly) you can animate inside drawRect - try it. You can fade, translate or animate any other property.
However, it starts "from fresh", from blank. So for example if you put a fade-in animation block in drawRect, the previous scene with disappear and the new scene will fade up from white.
I want the screen to fade from the previous image (drawn in the previous cycle of drawRect) to the new image I have just drawn ... err, am drawing.
Is there a way to do that, perhaps trickily by manipulating what's going on with drawRect?
This would seem to be a very common use case - blending from one scene to the next.
Does anyone know the secret?
Of course, obviously this can be done in the core animation milieu or in many other ways, but having drawRect fade from one drawRect to the next is an obvious idea. Cheers.
Astounding update thanks to the genius of WrightCS.....
Thanks only to WrightCS, we now know that drawRect handles animations perfectly. Simply paste this code at the end of any drawRect and try it:
self.alpha = 0.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
self.alpha = 1.0;
[UIView commitAnimations];
it treats the entire drawRect, no matter how complex, as one enormous block and it wraps it in that animation. Yes, it even includes painted in offscreen areas, bitmap rendering or anything else. Everything gets animated. Who knew?
The problem at hand - how to make it start the animation from the previous scene rather than start from blank?
Drawrect Is invisible. It happens in the 'backbuffer' which the iOS displays on screen only when you're ready with drawRect. So you can definitely not animate while in drawrect. However, you can commit animation instrucions from just about anywhere, including drawrect. But the animation will be performed afterwards.
Animation requires timing and showing different frames to the user.
You CAN do that all by yourself (constantly forcing a redraw and doing something slightly different in drawrect each the time) but that's a lot of work, especialy if you want it done right.
Luckily iOS has many animation effects programmed for you. Either using Core Animation, or the (more simple and basic) animation in UIKit. But since it works by animating certain properties of views (eg the alpha of a whole view, or the rotation of a whole view, ...) you might need to rearrange your views and subviews to make good use of it.
E.g. Each horse limb is separate subview and you animate their transformations (no redraw needed, iOS will do the rest)
E.g. The old and new frame are two separate views and you animate the new frame (which is on top) from alpha 0 to alpha 1.
You can animate the alpha:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
myObject.alpha = 0.0;
[UIView commitAnimations];
if you are under iOS 5.0+, you can use the following:
...
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
//do your stuff here
}completion:^(BOOL finished) {
//completition stuff go here
}];
...
The best thing you can do is to use two UIView instances and animate between them.
When you are starting the animation you have to know which is the original and final state of the animation. That's difficult if it's only one view.

Problems with auto-rotation and UIImageView animations

I am rotating a UIImageView in place periodically. My view is very basic, a view inside of a UITabBar view set. If I happen to rotate my iPad while my rotation is animating then my image becomes skewed. I have checked everything I can think of in my xib file for my image, the autoresizing is turned completely off and I am not auto-resizing subviews on the parent view.
Here is my animation code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
transform = CGAffineTransformMakeRotation(convertToRadian(myDegrees));
myImage.transform = transform;
[UIView commitAnimations];
If I take my animation out then everything works as I would expect. This rotation code appears to work fine if I do not rotate my device.
What can I do to keep the built-in rotation animation from altering my animation and skewing my images?
After many different attempts, what I ended up doing here is adding a clear view with my rotating images inside of it and setting the "auto-resize sub views" to off in interface builder. You can also do this in code as needed. I had to add the UIView placeholder because the super view containing my rotating images needed to auto-resize other view objects on rotation, but this fixed the funky skewing of my images due to animation of orientation change at the same time as animating a manual rotation in place.
Hope this helps someone.
The coordinate system gets changed. I've figured this much out too... thought I had a bug. If you save the CG ... GState, then you can revert back to the coordinate system initially used. This is in Deitel and Deitel's book. I just found it. Wanted to post it for anyone also looking.
You got a clever trick though! I might use it if this won't work.