Problems with auto-rotation and UIImageView animations - iphone

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.

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)

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.

don't allow subviews to scale vwith view?

i am scaling UIView using
[UIView setTransform:CGAffineTransformMakeScale(2.0*scale.value, 2.0*scale.value)];
its works fine for me but all subviews are also scale with the UIView, but i don't want to scale all subviews of that UIView.
i tried following this to stop scale subviews.
[UIView setAutoresizesSubviews:NO];
UIView.autoresizingMask = UIViewAutoresizingNone;
but still it scale subviews.
please help me.
Transformation works on the whole view as it is. There is no way to take things out of it.
Using transform matrices don't alter the size of your views directly, it just determines how the whole thing thing is then rendered on the screen.
If you just scale it by a factor of two, you won't gain or lose any level of detail either. Maybe set its frame instead.

Get a image from a uiview

I want to perform a shrink animation on a UITableVIew. I experimented a bit and found out that the animation runs much faster when I shrink a UIImageView with an image of the current state of the tableview instead of shrinking the table view itself.
I grabbed the image in a method in my main viewcontroller prior to the animation:
UIGraphicsBeginImageContext(mainTableView.bounds.size);
[resizeContainer.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Works like a charm, at least almost. On very rare occasions I get weird graphic glitches, where the UIImage starts to overlap a toolbar that lies underneath it.
I just want to make sure that I am getting the image in the right way. I am laking the necessary understand of GraphicContexts to be sure about it.
To cut a long story short, is my code correct?
Thx
In what way did you animate the table? I haven't had problems with it animating slowly. Here is an example of what I did when I had to resize my table:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[mainTableView setFrame:CGRectMake(0,43,320,176)];
[UIView commitAnimations];
The reason I would suggest not just taking a screenshot but actually animating the table is because the animation could possibly start as the table is still scrolling. For instance the user just happened to flick the table as it started animating then you would have a jump to the new state when you fade out the "screen shot" that you took.
It went away with iOS 4.0. So I guess it was a bug.

Getting started creating custom view transitions

I'm looking for tutorials on creating custom view transitions.
In particular, transitions that involve elements other than just the UIViews being affected, such as playing an animation over the transition as it is happening or modifying a screenshot of the UIView being transitioned out.
I don't mean implementing the basic set of transitions (slide, fade, etc) for which there's plenty of examples on Apple's site. I'm talking about adding video/sound/additional animation while wrapping it all in a reusable transition.
I'm vaguely familiar with some of the underlying toolkits (core-animation and quartz) but I'm looking for a no-prior-experience tutorial on the subject.
I have just put together a transition class to implement your own animation in OpenGL ES.
Feel free to read about it here
CoreAnimation animations work by recording differences in a very specific subset of UIView properties. They do not respond to any user-defined properties, and simply don't do more advanced transitions than you've already seen in the demos. You could take a screenshot of your current view, save it in memory as a texture, hide the old view and simultaneously show an OpenGL view. Then using the screenshot texture and various mesh animations, you could render your custom transition (including alpha blending around the crumpled/folded edges), then dispose of the OpenGL view to fully reveal the target view. Seems like a fun project, and you'd be in rare company to accomplish it.
Here is a quick guide to using the standard UIView transitions.
http://chris-software.com/index.php/dev-center/view-transitions/
If you want to fade into a view use something like this. This works with any type of element with an alpha property, such as UIImageViews, etc.
someView.alpha = 0;
[viewController.view addSubview:someView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationDelegate:self];
someView.alpha = 1;
[UIView commitAnimations];