change counterclockwise to clockwise with CGAffineTransformIdentity - iphone

I have a telephone wheel. On the touchend, it goes to his position with an animation.
Until the angle is less than 180°, it returns clockwise. No problem, with this code :
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.5];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
But It goes wrong after that and continue to rotate for a complet turn.
I tried to make to animations like this :
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
wheel.transform = CGAffineTransformRotate(wheel.transform, degreesToRadians(-130));
[UIView commitAnimations];
[self performSelector:#selector(animatewheelViewToCenter) withObject:nil afterDelay:0.3];
}
- (void)animatewheelViewToCenter{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.3];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
It works, but the animation isn't fluid ; the change is visible.

I'm not sure what the state is when touchesEnded (in terms of rotation) and I'm assuing you chose degreesToRadians(-130) to try and do it partially and expect the next method to do the rest. This should be a better solution that hopefully yields the result you're expecting. I'm not sure what cadran is or why you're rotating that, so I'll just rotate the wheel.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
wheel.transform = CGAffineTransformMakeRotation(degreesToRadians(-130));
// I would recommend that the degrees you rotate be half of whatever your rotation is.
// this would make the wheel rotate to the home position more evenly
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animatewheelViewToCenter:finished:context:)];
[UIView commitAnimations];
}
- (void)animatewheelViewToCenter:(NSString *)animationID finished:(NSNumber *)finished context:(id)context {
[UIView setAnimationBeginsFromCurrentState:YES]; // you might need to make this NO
[UIView beginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:0.2];
wheel.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
EDIT: Actually, I would probably make the rotation (in the example -130 degrees) slightly more than what half would be, because CGAffineTransformIdentity is gonna take the shortest path to go back to regular, so if you go exactly 1/2 way, it may not go the correct direction (clockwise or counter-clockwise) that you want.

Related

animations on infinite loop

I have 5 different animations that animate then disappear one after another. Is there a way to put them on an infinite loop so animation #1 begins and ends, then anim #2 begins and ends etc..? the whole process then would repeat on an infinite loop.
I have the animations in separate blocks on delays. I'm guessing that there is a better way of doing this. this is what I have at the moment:
-(void) firstAnimation {
[UIView beginAnimations:#"Fade Out Image" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:40];
[UIView setAnimationRepeatCount:30];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
theImage.alpha = 1;
theImage.alpha = 0.1;
[UIView commitAnimations];
[self secondAnimation];
}
-(void) secondAnimation {
tapImage2.hidden = NO;
[UIView beginAnimations:#"Fade Out Image" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:53];
[UIView setAnimationRepeatCount:29];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
theImage2.alpha = 1;
theImage2.alpha = 0.1;
[UIView commitAnimations];
[self thirdAnimation];
}
this then goes on for 5 animations. thanks for any help.
Instead of using the delay, set the animation delegate and create an animation done method. I notice you are already setting the delegate.
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
//Start next animation based on the animationID of the last one...
}

Triggering other animation after first ending Animation (Objective-C)

I have a simple animation which simply modify the position of a button:
[UIView beginAnimation:nil context:nil];
[UIView setAnimationsDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
mybutton.frame = CGREctMake(20, 233, 280, 46);
[UIView commitAnimations];
I want to perform some other animations when this one is finish, how to do that?
You could look at setAnimationBeginsFromCurrentState:. All the animations are run in their own threads, so [UIView commitAnimations] is a nonblocking call. So, if you begin another animation immediately after committing the first one, after appropriately setting whether or not it begins from the current state, I think you'll get the behavior you want. To wit:
[UIView beginAnimation:nil context:nil];
// set up the first animation
[UIView commitAnimations];
[UIView beginAnimation:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:NO];
// set up the second animation
[UIView commitAnimations];
Alternately, you could provide a callback by doing something like
[UIView beginAnimation:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)
// set up the first animation
[UIView commitAnimations];
//...
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// set up the second animation here
}
a code segment for getting start.. setup initial (the first) animation:
- (void) aniPath:(AnimationPath *) path
{
[UIView beginAnimations:path->aniname context:path];
[UIView setAnimationDuration:path->interval];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(aniDone:finished:context:)];
// your initial animation
[UIView commitAnimations];
}
chains to another animation when the first is done:
- (void) aniDone:(NSString *) aniname finished:(BOOL) finished context:(void *) context
{
AnimationPath *path = (AnimationPath *) context;
if ([aniname isEqualToString:path->aniname]) {
[UIView beginAnimations:path->aniname context:context];
[UIView setAnimationDuration:path->interval];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(aniDone:finished:context:)];
// more animations, even recursively
[UIView commitAnimations];
}
}
Use the +[UIVIew setAnimationDelegate:] and +[UIView setAnimationDidStopSelector:] methods to configure your class to receive a message when the animation ends.
it quite easier.
you can just
- (void)animationDidContiune:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIView *currentObj = context;
[self callTheAnimation: currentObj];
}
you can easily loop the animation.
To end it - I would create an animationDidEnded and use it instead of animationDidContiune
once you want to stop it (like simple if in animation that chooses either to contiune or end).
HF.
AND WHAT's IMPORTANT:
use this type of animating meths:
- (void)callTheAnimation:(UIView*)itemView
{
[UIView beginAnimations:#"animation" context:itemView];
.
.
.
}
they are quite flexible - you can animate whatever here due to hierarchy of the objects.

How can I use this animation code?

I'm a newbie and I need some help.
I want to display a popup image over a given UIView, but I would like it to behave like the UIAlertView or like the Facebook Connect for iPhone modal popup window, in that it has a bouncy, rubbber-band-like animation to it.
I found some code on the net from someone who was trying to do something similar. He/she put this together, but there was no demo or instructions.
Being that I am so new, I don't have any idea as to how to incorporate this into my code.
This is the routine where I need the bouncy image to appear:
- (void) showProductDetail
{
. . .
////////////////////////////////////////////////////////////////////////
// THIS IS A STRAIGHT SCALE ANIMATION RIGHT NOW. I WANT TO REPLACE THIS
// WITH A BOUNCY RUBBER-BAND ANIMATION
_productDetail.transform = CGAffineTransformMakeScale(0.1,0.1);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
_productDetail.transform = CGAffineTransformMakeScale(1,1);
[UIView commitAnimations];
}
. . .
}
This is the code I found:
float pulsesteps[3] = { 0.2, 1/15., 1/7.5 };
- (void) pulse {
self.transform = CGAffineTransformMakeScale(0.6, 0.6);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:pulsesteps[0]];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(pulseGrowAnimationDidStop:finished:context:)];
self.transform = CGAffineTransformMakeScale(1.1, 1.1);
[UIView commitAnimations];
}
- (void)pulseGrowAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:pulsesteps[1]];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(pulseShrinkAnimationDidStop:finished:context:)];
self.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];
}
- (void)pulseShrinkAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:pulsesteps[2]];
self.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
Thanks in advance for any help that you can give me.
It's pretty simple. In your showProductDetail method, you start an animation block, then set the _productDetail.transform property, and then commit the block. This is what makes the animation happen.
The code that you found is designed to do a chain of such animations, but the property being modified is on self instead of on _productDetail. If _productDetail is an instance of a class that you created yourself, you can put the animation code inside that class.
Otherwise, just move the code inside the pulse method to the showProductDetail method, and put the two other methods below that method. Replace self.transformwith _productDetail.transformin all three methods.

Want a flickering UIImageView (CoreAnimation and alpha-value)

I would like to have an UIImageView that flickers. I thougt I can make it with CoreAnimation and the alpha-value. I tried this:
for (int a = 1; a <= 100; a++) {
schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 0,1;
[UIView commitAnimations];
}
but it doenst work. He just moves to 0.1 and not again to 0.7.
I also tried this:
schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 0.1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 1;
[UIView commitAnimations];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 3;
[UIView commitAnimations];
// and so on...
And again It doenst work. How can I implement the flickering?
Thanks!
The problem with your code is that the [UIView commitAnimation] method doesn't block - I mean that the code implementation continues and the animation is done asynchronously.
So, actually, what is going on is that you go through all the loop iterations first and then the animation is done from 0.7 to 1.0...
Just use the setAnimationDidStopSelector without the "for" loop.
schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
schwarz.alpha = 0.1;
[UIView commitAnimations];
The catching method might be:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
/* Do your things here... */ }
All UIView animations added in one event loop are basically merged together. You need to use the UIView animationDidStopSelector. As an example, see the following:
-(void)tileAnimate:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
int position = [animationID intValue];
NSString *next = [NSString stringWithFormat:#"%d", position+1];
if(position == boardSize) {
return;
}
[UIView beginAnimations:next context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:timing];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(tileAnimate:finished:context:)];
buttons[position].transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
I use this to, one after another, animate shrinking an array of buttons back to their normal sizes.
Two more things:
First, UIViewAnimationCurveEaseIn does not work fading i think.
Second, in your original code you say:
schwarz.alpha = 0,1;
Note that there is a comma there and not a dot. Interestingly that code compiles, but it probably does not do what you intended.
Here is another take on this. I'm not using fading but I think the flickering effect is still nice.
http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/Flicker/
Use the first code sample posted, but take out the for statement and add the lines
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:(float)number];
I think -1 for the number should be repeating indefinitely, but I'm not 100% sure on that.

View rotation in touchesMoved

I rotate a view by 45 degree on every dragging. Here is my code. Problem here is that the view is rotated only once. After that there is no rotation. What will be the reason? Any help
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0];
dialView.transform = CGAffineTransformMakeRotation(45* M_PI/180);
[UIView commitAnimations];
}
You can also use CGAffineTransformRotate to rotate from its present transform, as in:
dialView.transform = CGAffineTransformRotate(dialView.transform, 45 * M_PI / 180);
The rotation is always relative to 0. If you rotate to x several times it will rotate only the first time.
If you want to rotate 45 everytime you call that code, you will need to have counter and it would look like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
step++;//You are supposed to keep this variable global.
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0];
dialView.transform = CGAffineTransformMakeRotation(45*step* M_PI/180);
[UIView commitAnimations];
}