I have a UIButton that when pressed, will make a UIImageView scale slightly larger and then back to its normal size. It's working, but I'm running into a problem where the image eventually keeps getting slightly smaller the more you press the button.
How can I change the code so that the image doesn't get smaller as you press the button? Here's the code I have to scale the image slightly larger and then back to normal:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 1.03, 1.03);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 0.97, 0.97);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
Thanks for any help.
It's math. Scaling something by 1.03 * 0.97 results in scaling factor of 0.9991 and not 1.0. Either use 1.0/1.03 as your second scaling factor or just set myImage.transform to the identity transform (assuming you are not applying other transformations to that view).
Have you tried saving first transform?
And then instead of using CGAffineTransformScale to shrink your UIImageView you just use your saved transform?
CGAffineTransform firstTransform = myImage.transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 1.03, 1.03);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = firstTransform;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
You are suffering from a limitation in floating point arithmetic and cumulative matrix operations. You will never be able to represent 3/100, only approximate it with values like, 0.03, 0.033, and 0.0333.
For your problem it would be better to set the second transform to the CGAffineTransformIdentity
Related
I use the code below to scale my view when event happen. But I want the view to be enlarged or reduced with animation not directly see the scaled result. How can I achieve that effect?
view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
You can use UIViewAnimations
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
[UIView commitAnimations];
You could also use a block animation.
[UIView animateWithDuration:0.3 animations:^{
view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
}];
I want to apply animation on UILabel text. I write the code to increase font size in animation block but animation is not applied.
[UIView beginAnimations:nil context:nil/*contextPoint*/];
monthsOnBoard.font=[UIFont fontWithName:#"digital-7" size:150];
daysOnBoard.font=[UIFont fontWithName:#"digital-7" size:150];
hoursOnBoard.font=[UIFont fontWithName:#"digital-7" size:100];
minutesOnBoard.font=[UIFont fontWithName:#"digital-7" size:100];
secondsOnBoard.font=[UIFont fontWithName:#"digital-7" size:100];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView commitAnimations];
The font of a UIView is not an animatable property. You should use transforms instead.
[UIView beginAnimations:nil context:nil/*contextPoint*/];
monthsOnBoard.transform = CGAffineTransformMakeScale(2.0, 2.0); //increase the size by 2
//etc etc same procedure for the other labels.
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView commitAnimations];
similarly, you can play with the values in CGAffineTransformMakeScale(x, y); - x is the horizontal scale constant and y is the vertical one. Enjoy!!
it may be help you
monthsOnBoard.transform = CGAffineTransformScale(monthsOnBoard.transform, 1, 1);
[UIView beginAnimations:nil context:nil/*contextPoint*/];
monthsOnBoard.transform = CGAffineTransformScale(monthsOnBoard.transform, 4, 4);
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView commitAnimations];
Well I would like to have two UIView animation on a same image in the same method like this :
-(void)likeThis{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
image.alpha=0;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
image.transform = CGAffineTransformScale(5,5);
[UIView commitAnimations];
}
But there is only one of these UIView Animation who work.I don't know why.I think there is another way to put two animation for the same image but I don't know how. sorry for my english I'm french :/
You can just put them in the same animation block:
-(void)likeThis
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
image.alpha=0;
image.transform = CGAffineTransformScale(image.transform,5,5);
[UIView commitAnimations];
}
Note that CGAffineTransformScale takes three arguments:
CGAffineTransformScale(image.transform, 5.0, 5.0)
See http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/doc/uid/TP30000946-CH1g-F16985
Or you could use CGAffineTransformMakeScale:
http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/c/func/CGAffineTransformMakeScale
Do you want them to happen simultaneously, or one after the other? If the former just include them both between a single set of beginAnimations..commitAnimations calls
Need to figure some transformation codes to implement falling down naturally just like leaves falling down from tree.
Here are some codes, fly from one place to another place with some rotation angle.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:5.0];
[UIView setAnimationDelegate:self];
//[UIView setAnimationRepeatCount:1e100f]; //coutless
[UIView setAnimationRepeatCount:1]; // 1 time
//[UIView setAnimationRepeatAutoreverses:YES];
leaf2.frame = CGRectMake(LEAF2_X, LEAF2_Y, LEAF2_W, LEAF2_H);
leaf2.transform = CGAffineTransformMakeRotation(ANGLE);
[UIView commitAnimations];
See if the next 2 questions help:
What is the best way to make a bouncing ball animation with infinite loop on iPhone?
Mimic UIAlertView Bounce?
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.