UIButtons will not fade out during animation block - iphone

I have a set of UIButtons and UILabels that I want to fade out when one button is selected and it is the correct button.
I've tried a number of things (commented them in the code block) and only the UILabels fade out. What am I missing here?
-(IBAction)answerButtonPressed:(UIButton *)sender {
NSLog(#"Game Question Answer Pressed: %i", sender.tag);
NSLog(#"%#", sender.titleLabel.text);
int selectedAnswer =0;
selectedAnswer = [question.answer intValue];
if (selectedAnswer == sender.tag) {
NSLog(#"GameQ %# is the correct answer", sender.titleLabel.text);
//self.toggleView;
[labelA setAlpha:0];
[labelB setAlpha:0];
[labelC setAlpha:0];
[labelD setAlpha:0];
/*
[buttonA setAlpha:0];
[buttonB setAlpha:0];
[buttonC setAlpha:0];
[buttonD setAlpha:0];
[buttonA setHidden:YES];
[buttonB setHidden:YES];
[buttonC setHidden:YES];
[buttonD setHidden:YES];
*/
[sender setAlpha:1];
[sender setHidden:NO];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
[UIView animateWithDuration:2.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{buttonA.alpha = 0;}
completion:nil];
[UIView setAnimationDelegate:[UIApplication sharedApplication]];
[UIView setAnimationDidStopSelector:#selector(endIgnoringInteractionEvents)];
[UIView commitAnimations];
I have since cleaned up the method and utilize only one type of animation block. The UIButton still will not fade out, but the label does. Here is what I have as the animation block:
[UIView animateWithDuration:2.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{buttonA.alpha = 0;}
completion:nil];
[UIView animateWithDuration:2.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{labelA.alpha = 0;}
completion:nil];

I couple things I notice off the bat. You are combining two types of animation techniques. Old and new:
Either do:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
buttonA.alpha = 0;
[UIView commitAnimations];
OR (and preferred as it is the "modern" way)
[UIView animateWithDuration:2.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{buttonA.alpha = 0;}
completion:nil];
Combining the two, as you have, would probably cause the block to be called many times as the first animation technique interpolated the second. Basically, putting many many animations in the queue, fading the button to 0 VERY fast.
Additionally, by default (using the block at least) user interaction is disabled by default. No need to do it explicitly.

Related

UIView Animation Block Makes No Changes

The following code is sheer simple. _facebookF is a UIImageView.
if (animationOn == YES) {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
}];
[_faceBookF setImage:[UIImage imageNamed:#"facebookFBlue.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
} else {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
}];
[_faceBookF setImage:[UIImage imageNamed:#"facebookF.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
}
The app gets to the method which contains this code fine, but no changes to the UI occur throughout the entire block of code. To my knowledge, the app is running on the main thread.
Any guesses?
UPDATE--In fact, the issue is that the image is not being changed at all--with or without the animation. Both images are accessed fine in other parts of the app.
It looks like you are trying to run sequential animations, but the way you have coded them, they are likely to run in parallel. What you need to do is this:
if (animationOn == YES) {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
} completion:^(BOOL finished) {
[_faceBookF setImage:[UIImage imageNamed:#"facebookFBlue.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
}];
}
else {
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:0];
}completion:^(BOOL finished) {
[_faceBookF setImage:[UIImage imageNamed:#"facebookF.png"]];
[UIView animateWithDuration:0.4 animations:^{
[_faceBookF setAlpha:1];
}];
}];
}
Try this. This starts the second animation after the first has completed. So it will fade out the UIImageView and then fade it up with different version of the image.
[UIView animateWithDuration: 0.4
animations:^
{
[_faceBookF setAlpha:0];
}
completion:^(BOOL finished)
{
[_faceBookF setImage:[UIImage imageNamed:#"facebookFBlue.png"]];
[UIView animateWithDuration:0.4
animations:^
{
[_faceBookF setAlpha:1];
}];
}];
Thanks for your responses. I've implemented the completion block you both mentioned to avoid both animations firing at once. However, the main issue had to do with the method not being accessed properly. I can't see exactly why, as the facebookF variable was not nil, and the if and log statements in the method worked fine. Anyway, I have switched to a NSNotification solution, and now all parts of the method are being accessed.

UIView animation for CGAffineTransformMake not working

I have a simple transform animation that moves a UITableViewCell 70 pixels to the left. In the if part, the animation works just fine and I get a smooth transition. However, when the function is called again to put the cell back in its original position, which is the else part, I get no animation. It just returns back to normal but without animation. How can I fix this?
if([[slideArray objectAtIndex:indexPath.row] intValue]==0)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
selectedCell.transform=CGAffineTransformMakeTranslation(-70, 0);
[UIView commitAnimations];
[slideArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:1]];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
selectedCell.transform=CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
[slideArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:0]];
}
try the following animation in else condition this will work fine if you alreadyb have perform translation of if condition....
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
selectedCell.transform=CGAffineTransformMakeTranslation(70, 0);
[UIView commitAnimations];
[slideArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:0]];
if above code not working for you than try the following
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:(void (^)(void)) ^{
myImageView.transform=CGAffineTransformMakeTranslation(-70, 0);
}
completion:^(BOOL finished){
myImageView.transform=CGAffineTransformIdentity;
}];
[slideArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:0]];
let me know if any issues ..
regards.

How to unhide view with animations

Say I have a hidden view in Xcode for iOS. Now, when I set the view to not hidden (view.hidden=NO), how can I make it so that it now appears, but with animations?
What you probably want is not to set view.hidden, but to set view.alpha to 0 (corresponds to hidden = YES) or 1 (hidden = NO).
You can then use implicit animations to show the view, e.g
[UIView animateWithDuration:0.3 animations:^() {
view.alpha = 1.0;
}];
If you want other animations than only fading then use this method
[UIView transitionWithView:_yourView duration:1.0 options:UIViewAnimationOptionTransitionCurlDown animations:^(void){
[_yourView setHidden:NO];
} completion:nil];
For a fade, you can adjust the alpha property of the view.
myView.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
myView.alpha = 1;
}];
That will apply a fade in over 0.5 seconds to the view myView. Many UIView properties are animatable; you aren't just limited to alpha fades. You can change background colours, or even rotate and scale a view, with animation. If you need further control and advanced animation, you can then move into Core Animation - a much more complex animation framework.
-(void)showView{
[UIView beginAnimations: #"Fade Out" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
//show your view with Fade animation lets say myView
[myView setHidden:FALSE];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(hideView) userInfo:nil repeats:YES];
[UIView commitAnimations];
}
-(void)hideView{
[UIView beginAnimations: #"Fade In" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
//hide your view with Fad animation
[myView setHidden:TRUE];
[UIView commitAnimations];
}
OR you can try this way
self.yourView.alpha = 0.0;
[UIView beginAnimations:#"Fade-in" context:NULL];
[UIView setAnimationDuration:1.0];
self.yourView.alpha = 1.0;
[UIView commitAnimations];

Problems moving animation to iOS4 blocks

I have a working view animation, that curls up a container view, while the containerview.subviews changes. (before the animation a UITableView will be shown, after it is a custom view, name keypadView)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:containerView
cache:YES];
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
[UIView commitAnimations];
Now I want to rewrite this code for the iOS4 block-based api, as I want to use the completion block. I wrote this:
[UIView transitionWithView:containerView
duration:.75
options:UIViewAnimationTransitionCurlUp
animations:^{
NSLog(#"Hey Ho");
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
}
completion:NULL];
The views switch — but not animated.
what is wrong with my code?
Edit
completion: ^(BOOL completed){
NSLog(#"completed %d", completed);
}
doesn't help, as NULL is an accepted value, according to the docs
do:
options:UIViewAnimationOptionTransitionCurlUp
instead of:
options:UIViewAnimationTransitionCurlUp
That is why your code works now :).
The sample in the UIView class reference may be wrong - or maybe there's a bug with adding and removing views in the animations block object, but the only way I've been able to get it to work is as follows:
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
[UIView transitionWithView:containerView
duration:.75
options:UIViewAnimationOptionTransitionCurlUp
animations:^{}
completion:^(BOOL finished) {
NSLog(#"finished %d", finished);
}];
Did you leave [UIView beginAnimations:nil context:nil]; above your new block?
Is the completion block always NULL? Try putting an NSLog statement in there or something. I don't know if NULL blocks would mess it up.

fliping view continiously

this my code
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
if ([sender tag] == 1) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:placeholder cache:YES];
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
}
if (view1OnTop) {
[view1 removeFromSuperview];
[placeholder addSubview:view2];
}
else {
[view2 removeFromSuperview];
[placeholder addSubview:view1];
}
[UIView commitAnimations];
view1OnTop = !view1OnTop;
i want to continuously flip the view, for say some duration 1 min.
it should be continuously flipping.
how can i do it.
what i m trying to is, i want a view, which should be continuously flipping for some particular amount of time.
how can i achieve this?
regards
Apart from the flipping animation, which I presume you have working, you need to initiate a new animation when the current one is finished.
Before [UIView commitAnimations], do this:
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDone:finished:context:)];
add a function
-(void)animationDone:(NSString*)id finished:(NSNumber*)n context:(void*)context
and let that fire the next round.
edit: you do this by putting in the code to fire an animation, so the typical block from [UIView beginAnimations...] to [UIView commitAnimations]. The better solution of course is to put the animation starting code in a separate function, so the outline will look like:
...
[self startAnimationLoop];
...
-(void)startAnimationLoop
{
[UIView beginAnimtions...];
// do the animation stuff
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDone:finished:context:)];
[UIView commitAnimations];
}
-(void)animationDone:(NSString*)id finished:(NSNumber*)n context:(void*)context
{
[self startAnimationLoop];
}
to make it go back/forth, add some state variable, or create 2 sets of these functions which call eachother (startAnimationLoop1 and startAnimationLoop2, each one firing the other when done)