Trying to perform multiple animations at once - ios

Seems strange that i could not find any answer for this all over the net, but seems that if you want to move 6 UIViews at the same time in a different speed ,you can't do that.
If i am using one of this 2 example, i get that sometimes only some of the views are moving, and sometimes all of them (as expected) .
There is no way to move 6-7-8 UIViews at the same time ,with different duration?
1.
for(UIButton *button in buttons)
{
float r= arc4random()%10;
float t =0.1+ 1.0/r;
[UIView beginAnimations:#"Anim0" context:nil];
[UIView setAnimationDuration:t];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:button cache:YES];
CGRect newframe=button.frame;
newframe.origin.y=0;
button.frame=newframe;
[UIView commitAnimations];
}
2.
for(UIButton *button in buttons)
{
int random=arc4random()%10;
float time=0.5+ 1/(float)random;
CGRect newframe=button.frame;
newframe.origin.y=0;
[UIView transitionWithView:button
duration:time
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
button.frame=newframe;
}
completion:nil];
}

You need to limit the random number generator.
Your function...
arc4random() % 10;
Will sometimes return 0.
Then passing this into...
float time = 0.5 + 1/random;
Will make time = inf.
You can see this by logging out the time values.
A time of inf will make the button not animate.

Of course it is possible. The problem is that you're sending the wrong message. Based on your second snippet, this is an example of how it could be done:
for (UIButton *button in self.buttons) {
int random = (arc4random() % 10) + 1; // "+1" Added later in the answer, see Fogmeister's answer
float time = 0.5 + 1 / (float)random;
CGRect newframe = button.frame;
newframe.origin.y = 0;
[UIView animateWithDuration:time
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
button.frame = newframe;
} completion:^(BOOL finished) {
// The animation have finished
}];
}

Related

Smoothest Way To Implement Back to Back UIAnimations

I need to have my character jump (increase the origin.y by 50 and reverse) in a game I am creating.
So far I have come across two ways of doing this:
Method 1:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionAutoreverse
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y -= 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
CGRect frame = self.kevinImageView.frame;
frame.origin.y = 135;
self.kevinImageView.frame = frame;
}];
Issues: At the end of every complete jump animation (up and down) the ImageView jumps up and then back down (due probably to the tiny amount it takes for the completion block to be called). This is noticeable and I would much rather do without it.
Method 2:
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y -= 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y += 50;
self.kevinImageView.frame = frame;
}];
}];
Issues: If I tap the view (I use a UITapGuestureRecognizer) and the ImageView is in the completion animation (going back down), the Image View will snap back to the bottom instead of finishing its animation. Again not a major issue, but something which I should be able to avoid.
Is there any method I have not come across which will resolve both of these issues, or a way to fix one of the methods?
Can you disable your UITapGuestureRecognizer when the animation is still running? And then enable it when the animation finishes.
if (!self.isAnimationRunning) {
self.isAnimationRunning = YES;
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y -= 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y += 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
self.isAnimationRunning = NO;
}];
}];
}
I'm very interested in this question so I do a simple demo to do a back to back animation.
I think maybe you can just use UIViewAnimationOptionAutoreverse and UIViewAnimationRepeat option to achieve a autoreverse animation.
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionAutoreverse|UIViewAnimationRepeat
animations:^{
[UIView setAnimationRepeatCount:1];
self.kevinImageView.center = CGPointMake(self.kevinImageView.center.x ,self.kevinImageView.center.y - 25);
} completion:^(BOOL finished){
self.kevinImageView.center = CGPointMake(self.kevinImageView.center.x ,self.kevinImageView.center.y + 25);
}];
and you can also set UIViewAnimationOptionAllowUserInteraction if you want to enable use interaction during the animation.

how to use array value in UIView animation

In UIViewAnimation, so far i used a single image to animate from one place to another like this:
CGRect frame = button.frame;
CGRect frame1 = button1.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView animateWithDuration:3.0 animations:^{
[button setTransform:CGAffineTransformIdentity];
} completion:^(BOOL finished) {
}];
button.frame = frame;
[UIView commitAnimations];
Here I have nine images in an array:
redAppleArray = #[redApple1,redApple2,redApple3,redApple4,redApple5,redApple6,redApple7,redApple8,redApple9,redApple10];
I have text display random numbers.
My question is how to get the above text value and animate the image using UIViewAnimation? i.e., if text =3, 3 redApple to animate .
To get the value from a label for example do:
int i = [[myLabel text] intValue];
And then use it to get the element in your array:
id *redApple = [redAppleArray objectAtIndex:i];
Try do something lilt this:
-(void)yourMethodToAnimateWithText:(NSString*)numberAsAText {
int = [numberAsAText intValue];
CGRect frame = button.frame;
CGRect frame1 = button1.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
// If you want to add some option use [UIView animateWithDuration:delay:option:animation:completion:]
[UIView animateWithDuration:3.0 animations:^{
//[button setTransform:CGAffineTransformIdentity];
button.frame = frame;
//Animate your images, text, etc.
} completion:^(BOOL finished) {
}];
}
Hope this is what you after.

UIView Animation Mac OSX Effect

I'm animating a small view with popup effect to alert you of an error ... The animation works perfectly but still are not very satisfied because I would like the view does not stop at position (x 25) but I wish they came to ( x 40 ) and then return immediately to ( x 25). In other words I would like to recreate the effect they have on the textfield MontainLion when you are wrong to insert the fields User for access to the operating system ...
I really hope to be able to explain to me , but if I did not succeed please comunicarmelo so that I can explain it better to resolve this question. Thanks to all! Rory .
Below I show you the code to display the pop-up I'm using
- (Void)presentazioneViewTransition
{
CGRect endREct ;
endREct = CGRectMake (25, 160, 270, 90);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(endRightAnimation)];
self.frame = endREct;
[UIView commitAnimations];
}
You can use the simple block based UIView animation methods like this
-(void)presentazioneViewTransition{
CGRect transitionRect = CGRectMake(40.0f, 160.0f, 270.0f, 90.0f);
CGRect endREct = CGRectMake (25.0f, 160.0f, 270.0f, 90.0f) ;
[UIView animateWithDuration:0.15f
animations:^{
self.frame = transitionRect;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.1f
animations:^{
self.frame = endREct;
}];
}];
}

How to lengthen the UISearchBar in x direction with animation?

I want to lengthen the right search bar 20 px to the left, and shorten left search bar 20 px to the right with animation. How can I do that?
CGRect leftFrame = self.leftSearchBar.frame;
//leftFrame.origin.x = leftFrame.origin.x - 20;
leftFrame.size.width = leftFrame.size.width - 40;
CGRect rightFrame = self.rightSearchBar.frame;
rightFrame.origin.x = rightFrame.origin.x - 40;
rightFrame.size.width = rightFrame.size.width + 40;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.leftSearchBar.frame = leftFrame;
self.rightSearchBar.frame = rightFrame;
[UIView commitAnimations];
It doesn't work. It first resizes views without animation, then moves the right view with animation.
Assuming you've not changed the anchor points, or anything like that, simply expanding the width will make it stretch to the right. Wrap this within a UIView animation block, and it should animate according to the specification within the question, as so:
UISearchBar* searchbar = ...; // However you've created your UISearchBar, we'll refer to it as 'searchbar'
CGRect searchFrame = searchbar.frame;
[UIView animateWithDuration:0.8
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
searchFrame.size.width += 30;
searchbar.frame = searchFrame;
}
completion:nil];
You'll need to make sure the += 30 doesn't get called more than once, as otherwise you'll be expanding by 30 points each time this is called. If you know the size before hand, you could simply substitute it with = size + 30, which is a safer bet incase you call the animation more than once.

UILabel always animates to same spot

I have a UILabel called "nameLabel" and I have it inside an animation block so that this happens:
_nameLabel.alpha = 1;
CGAffineTransform translate = CGAffineTransformMakeTranslation(50, 50);
_nameLabel.transform = translate;
I thought that animates the UILabel to the spot I specify but it just animates from the above spot to the place where I have it in the Interface Builder. Any help here?
Can you post the animation block and other associated code? I am not sure what CGAffineTransformMakeTranslation does with the label, but if you want to animate the frame location, you can use this:
CGRect frame = _nameLabel.frame;
frame.origin.y = 100; //Desired height, origin.x can be modified as well.
//You can also change the frame size here but it wont work on UILabels, you will need `CGAffineTransformScale` for that.
[UIView animateWithDuration: 1.5
delay:0.0
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
animations:^ {
[_nameLabel setFrame:frame];
}
completion:^ (BOOL finished) {
}];
Edit: The other way:
CGRect frame = _nameLabel.frame;
frame.origin.y = 100;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
[_nameLabel setFrame: newFrame];
[UIView commitAnimations];
Current state is probably the current location of the label, try that first but if nothing happens, remove UIViewAnimationOptionBeginFromCurrentState or set the frame of the label to another location before the animation, and move it to its initial (the one in the xib file) position in the animation block, its your call.
Watch out because Autolayout cause a lot of problems.
Try to deselect 'Use Autolayout'
It solves to me all the problems trying to translate objects.

Resources