We have seven buttons, on clicking of anyone among them, i need to perform animations on remaining buttons "in sequence".
At present am using the following code in for loop,but animation is done on all the buttons at once.
[UIView beginAnimations: #"Fade In" context:nil];
button.alpha = 0;
[UIView setAnimationDelay:0];
[button setTitle:#"helloworld" forState:UIControlStateNormal];
[UIView setAnimationDuration:0];
button.alpha = 1;
[UIView commitAnimations];
You should not use beginAnimations:context: any more. To quote Apple's docs:
Use of this method is discouraged in iOS 4.0 and later. You should use
the block-based animation methods to specify your animations instead.
Use the new animateWithDuration family of methods instead. Specifically animateWithDuration:delay:options:animations:completion:. That method takes a delay parameter. You can issue a series of animation commands, each with a larger delay value. You can also chain animations by making the completion block of each animation trigger the next animation.
Edit: here is a link to a post where I show code that uses the completion method to trigger a sequence of animations one right after the other:
Scrolling the array of images using the CABasicAnimation
Try this thing in for loop
for(int btnIndex = 0; btnIndex < 7; btnIndex++) {
UIButton *button = [buttons objectAtIndex:0]; //get button reference from an array
[UIView beginAnimations: #"Fade In" context:nil];
button.alpha = 0;
[UIView setAnimationDelay:(0.1 * btnIndex)];
[button setTitle:#"helloworld" forState:UIControlStateNormal];
[UIView setAnimationDuration:0.1];
button.alpha = 1;
[UIView commitAnimations];
}
Related
I have a animation view. When the animation is finished I would like to show a new view by setting its hidden to false.
I set the hidden to false after the animation block code, but it seems like the animation is being done on a separate thread. The view gets unhidden while the animation block is still playing
// start animation block
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut ];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:mCards[r] cache:YES];
// mCards[0].image = [UIImage imageNamed:#"card.png"]; //begin
int id=TarretWheel[r];
mCards[r].image = gCardImages[id]; //end
// Start animtion
[UIView commitAnimations];
// show view
mBackground.hidden=false;
You can (and preferably should, actually), be using the newer block-based animation methods on UIView. Check out the following (it has a completion block which is just what you need):
[UIView animateWithDuration:0.5 animations:^{
} completion:^(BOOL finished) {
}];
Edit:
There are other variants with options that you may want in your case.
Also, clarification on why you 'should' be using the block-based methods; from the docs (for beginAnimations:context:):
Use of this method is discouraged in iOS 4.0 and later. You should use
the block-based animation methods to specify your animations instead.
My answer is same as Joe but I would recommend using a different API which allows you with configure block.
[UIView animateWithDuration:2
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionCurveEaseOut |
animations:^{
// mCards[0].image = [UIImage imageNamed:#"card.png"]; //begin
int id=TarretWheel[r];
mCards[r].image = gCardImages[id]; //end
}
completion:^(BOOL finished){
mBackground.hidden=false;
}
];
I've been reading about UIView animateWithDuration which I'm trying to use so when a button is pressed a graphic appears then slowly fades out (i.e. alpha is set to 0).
I'm using the code below in my viewdidload just for test purposes however its not working:
[UIView animateWithDuration:10 animations:^{
self.completeImage.alpha = 1.0;
self.completeImage.alpha = 0.5;
self.completeImage.alpha = 0.0;
}];
Any ideas?
Thanks.
That is not working because automatically it sets the alpha to 0.0;
The 3 lines of code are executed at the same time (one after the other).
The proper way to use the UView animation block it is like this:
self.completeImage.alpha = 0.0;
[UIView animateWithDuration:2.0
animations:^{
// do first animation
self.completeImage.alpha = 1.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0
animations:^{
// do second animation
self.completeImage.alpha = 0.0;
}
completion:^(BOOL finished){
;
}];
}];
Hope this achieve what you are looking for.
In addition:
" I'm trying to use so when a button is pressed a graphic appears
then slowly fades out (i.e. alpha is set to 0)."
As per your above information in the question, addition of the code in viewDidLoad will not prove fruitful. You need to add this code in the action target method of your button in order to play the animation on click of a button. Generally if you're using the nib, then the action method will be like below:
-(IBAction)on_pressing_my_button:(id)sender
{
///your animation code goes here..
}
I want to make a button fade away after it is clicked. I know that I can use
_myButon.hidden = TRUE;
...to completely hide a button, but it feels abrupt and jarring. I also know I could sequentially lower the alpha or something, but wasn't sure how to make this happen automatically over a short period of time.
Can someone please give me a tip how to fade out a button after it is clicked using the simplest means possible? I want the effect to look something like a simple "Fade-out" from a powerpoint presentation or something :)
Thanks!
[UIView animateWithDuration:0.5 animations:^{
_myButton.alpha = 0;
}];
Rather than removing the button, just hide it. Taking all the suggestions into account you get:
[UIView animateWithDuration:0.5
animations:^{ _myButton.alpha = 0; }
completion:^(BOOL finished){ _myButton.hidden = YES; }
];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
_myButton.alpha = 0.0f;
[UIView commitAnimations];
alternatively
[UIView animateWithDuration:1.0 animations:^{
_myButton.alpha = 0.0f;
}];
Just reducing the alpha is not going to make your button completely be removed from your view. To a user it will look like its gone but its still there. They potentially still could accidently click it without knowing. So what you can do is do a timer to remove it from view after it has faded away.
...
//alpha animation
//remove from view
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:#selector(hideMyButton) userInfo:nil repeats:NO];
}
-(IBAction) hideMyButton
{
[_myButon removeFromSuperview];
}
I have two imageViews that I call firstView and secondView; I have to do an animation that move a third imageView that I call imageView3. This imageView3 is inside firstView and I do this code
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[imageView3 setFrame:frameinSecondView];
[UIView commitAnimations];
//[imageView3 removeFromSuperview];
//[secondView addSubview:imageView3];
with this code I can move imageView3 to secondView but the problem is that I'm not able to set imageView3 in the correct frame "frameinSeconView"; instead if I use the commented code
[imageToMove removeFromSuperview];
[scrollViewAlfabeto addSubview:imageToMove];
imageView3 go on the correct frame but I don't see the animation because when it starts its animation, imageview3 disappear from firstView ad appear when it go on secondView
How Can I solve to have a correct animation, with a correct frame between these two imageViews.
You are trying to pass imageView3 from 1 to 2 right?
Remove it from imageView1 and place it on the windowView, do the animation from 1 to 2 and wait to end the animations to remove from windowView and insert into imageView2
how to wait? ... well there is a [sentence something] structure that do that, but i dont know how is it, instead of that (cause ive never found how) i started to use "animation blocks" :D
what is an animation block?
block: object that means an action;
below is an example of an animation using block objects block1, block2 (CODE PARTC)
[UIView animateWithDuration:2 delay:0
options:UIViewAnimationCurveEaseIn
animations: block1
completion: block2
];
It means that it will do actions defined in block1, as if they were inside begin-commit animation sentences, and (the beautiful part) after its COMPLETION they will do the actions in block2! :D!
but what is a f----ng block?; well...
THIS IS A F----NG BLOCK!:
Block that returns void (CODE PARTA):
void (^block1)(void) = ^{
centerPortrait.frame = frame1;
if (lookingMenu==NO)
listTableView.frame = frameT1;
};
Block that returns BOOL (after completion: you need a bool) and that have another animation inside (CODE PARTB)
void (^block2)(BOOL) = ^(BOOL got){
[UIView animateWithDuration:halfDuration delay:0
options:UIViewAnimationCurveEaseIn
animations:^{
centerPortrait.image = image;
centerPortrait.frame = frame2;
if (lookingMenu==YES)
listTableView.frame = frameT2;
}
completion:nil
];
};
I've used this to make a fake rotation of an image hohohoho
Anyways if u didnt got it, the order is PARTA,B,C :P
Read this:
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html%23//apple_ref/doc/uid/TP40007502-CH7-SW1
is where i got how to use blocks :D, and heres an useful tutorial of the great RAYWENDERLICH:
http://www.raywenderlich.com/2454/how-to-use-uiview-animation-tutorial
Hope it helps!
I'm trying to add a continuous animation onto my UITableViewCell-Subclass.
It's a rather easy one with an Image fading in and out (fading between 0.4 alpha and 1.0),
what I've tried so far ist the following:
-(void)animateRecordingIndicator{
[UIView beginAnimations:#"RecordingIndicatorAnimation" context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished)];
if (animatedImageView.alpha == 0.4)
animatedImageView.alpha = 1.0;
else
animatedImageView.alpha = 0.4;
[UIView commitAnimations];
}
the code within animationFinished is as follows:
-(void)animationFinished{
if (doShowAnimation) {
[self performSelectorOnMainThread:#selector(animateRecordingIndicator) withObject:nil waitUntilDone:YES];
}
}
what I expect should be clear by now, but what I get is simply a crash with Xcode loading Stackframes more or less eternally :)
According to the UIView class reference, you are now discouraged from using the commitAnimations method. Instead use the following:
animateWithDuration:delay:options:animations:completion:
I imagine the infinite recursion you are encountering is related to Apple's reasons for making that recommendation.