The method im trying to achieve is set a button to 0.5 alpha then back to 1 in the space of three seconds. After its executed 5 time on the four buttons the block of code is finished. Im struggling to find a way in which this can be achieved becasue right now the block below will be an infinite loop when i want it to be only executed through once.
int rand=random()%5;
switch (rand) {
case 1:{
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnYellow setAlpha:0.5];
[btnYellow setAlpha:1];
[UIView commitAnimations];
}
break;
case 2:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnRed setAlpha:0.5];
[btnRed setAlpha:1];
[UIView commitAnimations];
break;
case 3:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnBlue setAlpha:1];
[UIView commitAnimations];
case 4:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnGreen setAlpha:1];
[UIView commitAnimations];
break;
case 5:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnYellow setAlpha:1];
[UIView commitAnimations];
break;
}
As you are setting up the button's alpha to 0.5 and then to 1 immediately, the button's alpha won't animate. You can get an idea this snippet
[UIView animateWithDuration:0.3 animations:^{
[your_btn setAlpha:0.5];
} completion:^(BOOL finished) {
if(finished)
[self performSelector:#selector(revertAlphaToOne) withObject:nil afterDelay:0.5];
}];
And in that revertAlphaToOne Method, you can revert the button's alpha to 1 as
[UIView animateWithDuration:0.3 animations:^{
[your_btn setAlpha:1.0];
} completion:nil
}];
Adjust the time variables according to your likings And/Or call the second snippet in the first block's completion block itself.
Well, I've been using this site for several years, and I was due for a nice thorough answer to give back.
I built a project and accomplished the functionality you desired. Some notes:
-The key is recursion! You need to animate the fade out (using the function I provided) and, upon completion, animating the fade back in using the same function. Once completed, exercise recursion and prepare to call itself (beginAnimations) until all ints=5.
-I decided to fade from 1->.5->1 because the .5 alpha was annoying me. Shouldn't be hard to switch that around if you want it reversed.
-To make sure no button fades more than fives times, you need to declare and increment an int corresponding to each button.
-Use arc4random(), not random(). Random is for debugging as you will have the same result every time.
-Keep your switch case's straight; was hard to understand what you wanted to be different between them. On this note, use breaks and a default statement! Good luck employing all this into your app.
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
//declare and define button specifics
btnYellow = [UIButton buttonWithType: UIButtonTypeCustom];
[btnYellow setBackgroundColor: [UIColor yellowColor]];
btnYellow = [self buttonTraits:btnYellow];
[btnYellow setFrame: CGRectMake(20, 20, 280, 30)];
[btnYellow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnYellow.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
btnRed = [UIButton buttonWithType: UIButtonTypeCustom];
[btnRed setBackgroundColor: [UIColor redColor]];
[btnRed setFrame: CGRectMake(20, 60, 280, 30)];
[btnRed setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnRed.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
btnBlue = [UIButton buttonWithType: UIButtonTypeCustom];
[btnBlue setFrame: CGRectMake(20, 100, 280, 30)];
[btnBlue setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnBlue.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[btnBlue setBackgroundColor: [UIColor blueColor]];
btnGreen = [UIButton buttonWithType: UIButtonTypeCustom];
[btnGreen setFrame: CGRectMake(20, 140, 280, 30)];
[btnGreen setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnGreen.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[btnGreen setBackgroundColor: [UIColor greenColor]];
//add buttons to the view
[self.view addSubview:btnYellow];
[self.view addSubview:btnRed];
[self.view addSubview:btnBlue];
[self.view addSubview:btnGreen];
//set the counting ints to 0
yellowCount = 0, redCount = 0, blueCount = 0, greenCount = 0;
//run through the animations the first time
[self beginAnimations];
}
-(void)beginAnimations
{
if (!(yellowCount==5 && redCount==5 && blueCount == 5 && greenCount == 5))
{
//if you want 5 buttons, define another one, then the next line would be int rand=random()%6;
int rand=((arc4random()%5)+1); //arc4random gives 0-3; add 1 for 1-4
switch (rand) {
case 1:
{
//make sure this button hasn't gone through the process 5 times already
if (yellowCount<5)
{
//increment the button's count
yellowCount++;
//set up animation with 1.5 second duration (alpha decline from 1->.5)
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnYellow setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnYellow setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
//restart the animation hoping to get another button that hasn't gone 5 times yet
[self beginAnimations];
}
}
break; //you forgot a break here. can cause troubles
case 2:
{
if (redCount<5)
{
redCount++;
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^
{
[btnRed setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnRed setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
[self beginAnimations];
}
}
break;
case 3:
{
if (blueCount<5)
{
blueCount++;
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^
{
[btnBlue setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnBlue setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
[self beginAnimations];
}
}
break; //you forgot another break here. can cause troubles
case 4:
{
if (greenCount<5)
{
greenCount++;
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^
{
[btnGreen setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnGreen setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
[self beginAnimations];
}
}
break;
default:
{
//in case of an awry number, restart the process (be wary; infinite loop potential)
[self beginAnimations];
}
break; //it is of good practice to always have a default method in switch statements
}
}
else
{
//the process is complete
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
UIButton *btnYellow;
UIButton *btnRed;
UIButton *btnBlue;
UIButton *btnGreen;
int yellowCount;
int redCount;
int blueCount;
int greenCount;
}
#end
The problem is that in cases 1 and 2, you set the alpha value to 0.5 and then IMMEDIATELY back to 1. So when the animation starts, it is simply 1 and will remain so. Just delete the 2nd assignment, and the animation will change alpha to 0.5. PS: As already mentioned, the random number generated is from 0 to 4, and not from 1 to 5.
Related
I have a UITextField that is set to hidden, when I click the search button I want to make it visible but I want to create an animation like a slide-in, is it possible to recreate this effect if i'm using autoLayout?
The code I used for the animation is the current (it creates a fade-in effect):
if(!currentSrcState)
{
_searchField.hidden = NO;
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^ {
_searchField.alpha = 1.0;
}
completion:nil];
currentSrcState = YES;
}
else
{
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^ {
_searchField.alpha = 0;
}
completion:^(BOOL finished){
_searchField.hidden = YES;
}];
currentSrcState = NO;
}
What I want to acheive is that when the _searchField is hidden the constraint gets recalculated in order to move what is under the searchField up
Instead of set the button to hidden, set its alpha value to 0.0 before load the view. Then change your code to:
if(!currentSrcState)
{
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^ {
_searchField.alpha = 1.0;
}
completion:nil];
currentSrcState = YES;
}
else
{
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^ {
_searchField.alpha = 0.0;
}
completion:nil];
currentSrcState = NO;
}
I am busy with a custom animated transition between UIViewControllers and I like the the UIView to jump and then fill up the space similar to the following screenshot:
Now there isn't an "Option" I could create such an effect. Now I could create a keyframe animation with one key at the top and one in the center but that wouldn't feel really natural, it would feel robotic.
How would one approach this?
SOURCE (SO FAR)
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *container = [transitionContext containerView];
if ([fromVC isEqual:self])
{
[_activeCardView.superview bringSubviewToFront:_activeCardView];
[UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext]
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
[_activeCardView setFrame:CGRectMake(0.0, 0.0, toVC.view.bounds.size.width, toVC.view.bounds.size.height)];
}
completion:^(BOOL finished){
[container addSubview:toVC.view];
[transitionContext completeTransition:YES];
}];
}
else
{
[container addSubview:toVC.view];
[UIView animateWithDuration:[self transitionDuration:transitionContext]
animations:^{
[_activeCardView setFrame:_basicCardFrame];
}
completion:^(BOOL finished){
[transitionContext completeTransition:YES];
}];
}
}
For a smooth 'jump' animation you could just chain two animations with 'EaseInOut' curves.
[UIView animateWithDuration:<DURATION>/2 delay:<DELAY> options:UIViewAnimationOptionCurveEaseInOut animations:^(void) {
//move view up
}completion:^(BOOL finished) {
[UIView animateWithDuration:<DURATION>/2 delay:<DELAY> options:UIViewAnimationOptionCurveEaseInOut animations:^(void) {
//move view down
}completion:NULL];
}];
How do you pause and resume UIView Animations? (without block animations)
I was having the hardest time figuring this out, so here is my source below on how to do it.
How to pause and resume UIView Animations:
This will cover how to do the above without block animations. (People do still wish to support 3.0 and up).
This only allows for pausing once the animation has met the set location. For how to pause an animation in the middle of an animation, i suggest using CALayers as such:
CALayer* myLayer = [self.myUIView.layer presentationLayer];
CGRect frameStop = myLayer.frame;
double pausedX = frameStop.origin.x;
double pausedY = frameStop.origin.y;
Now for the actually how to:
startAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect];
startAnimation.titleLabel.font = [UIFont systemFontOfSize:22];
startAnimation.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation;
[startAnimation setTitle:(#"pause") forState:UIControlStateNormal];
[startAnimation addTarget:self action:#selector(doAnimation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startAnimation];
-(void)doAnimation{
if (bicyclePad.animationPause == false){
[restartAnimation setTitle:(#"Resume") forState:UIControlStateNormal];
[bicyclePad pauseAnimation];
} else {
[restartAnimation setTitle:(#"Pause") forState:UIControlStateNormal];
[bicyclePad resumeAnimation];
}
}
-(void)resumeAnimation{
bicyclePad.animationPause = false;
[restartAnimation setTitle:(#"Resume") forState:UIControlStateNormal];
objectMotion = [[yourUIView alloc]initWithFrame:CGRectMake((*yourPathPoints)[0].x, (*yourPathPoints)[0].y, 8, 8)];
[self.view addSubview:objectMotion];
[self animateBike:nil finished:YES context:nil];
}
-(void)pauseAnimation{
bicyclePad.animationPause = true;
[restartAnimation setTitle:(#"Pause") forState:UIControlStateNormal];
[bicyclePad doAnimation];
}
-(void)animateObject:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
//below suggesting your animation loops
if (animationPause == true)
[UIView setAnimationDidStopSelector:#selector(animateStop:finished:context:)];
else
[UIView setAnimationDidStopSelector:#selector(animateObject:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView setAnimationDuration:yourDelay];
[UIView commitAnimations];
animationPause == false;
}
-(void)animateStop:(NSString*)animationID finished:(BOOL)finished context:(void*)context{
}
I had added auto scroll functionality to my application by going through sample code.
In this code I'm getting scroll from top to bottom and bottom to top. But I don't want to scroll from top to bottom . I always wants to scroll from bottom to top only. If I'm removing the topToBottom code and making changes to the remaining code according to that it is not working. Can any one help me.
-(void)viewdidLoad
{
infoScrollView = [[UIScrollView alloc]init];
infoScrollView.frame = CGRectMake(0,0,330,270);
infoScrollView.contentSize = CGSizeMake(100,1000);
[myInfoView addSubview:infoScrollView];
scrollingToTop=NO;
UIImageView *infoImgView = [[UIImageView alloc]initWithFrame:CGRectMake(70,0,200,1000)];
infoImgView.image = [UIImage imageNamed:#"lepke-tar2.png"];
[infoScrollView addSubview:infoImgView];
infoScrollView.delegate = self;
if (infoScrollView.contentSize.height>infoScrollView.frame.size.height)
{
[infoScrollView scrollRectToVisible:CGRectMake(0, infoScrollView.contentSize.height-infoScrollView.frame.size.height, infoScrollView.frame.size.width, infoScrollView.frame.size.height) animated:NO];
}
[self performSelector:#selector(scrollToTop) withObject:nil afterDelay:0.5];
[myInfoView addSubview:infoScrollView];
UIButton* infocancelBttn = [[UIButton alloc ]initWithFrame:CGRectMake(295,-8,45,45)];
[infocancelBttn setImage:[UIImage imageNamed:#"cancel_icon.png"] forState:UIControlStateNormal];
[infocancelBttn addTarget:self action:#selector(infoViewCloseAction) forControlEvents:UIControlEventTouchUpInside];
[myInfoView addSubview:infocancelBttn];
[infocancelBttn release];
}
-(void)viewDidAppear:(BOOL)animated
{
[infoScrollView scrollRectToVisible:CGRectMake(0, 0, infoScrollView.frame.size.width, infoScrollView.frame.size.height+200) animated:YES];
}
- (void) scrollToTop
{
scrollingToTop=YES;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10.0];
//[self.scrollView scrollRectToVisible:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:YES];
[infoScrollView setContentOffset:CGPointMake(0,-300)];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(scroll)];
[UIView commitAnimations];
}
-(void) scroll
{
if (scrollingToTop==YES)
{
[self performSelector:#selector(scrollToBottom) withObject:nil afterDelay:1.0];
}
else
{
[self performSelector:#selector(scrollToTop) withObject:nil afterDelay:1.0];
}
}
- (void)scrollToBottom
{
scrollingToTop=NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10.0];
[infoScrollView setContentOffset:CGPointMake(0,infoScrollView.contentSize.height-infoScrollView.frame.size.height)animated:NO];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(scroll)];
[UIView commitAnimations];
}
I think your if-statement is wrong:
-(void) scroll
{
if (scrollingToTop==YES)
{
[self performSelector:#selector(scrollToBottom) withObject:nil afterDelay:1.0];
}
else...
if you delete your scrollToBottom-method your code fails.
you should change this to
[self performSelector:#selector(scrollToTop) withObject:nil afterDelay:1.0];
because you are asking if scrollingToTop is true
-(IBAction)nextButtonAction:(id)sender{
UIView *newView=[[UIView alloc]initWithFrame:CGRectMake(0.0f,0.0f,320.0f,416.0f)];
UIImageView *newImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0.0,0.0, 320.0,416.0 )];
UIImageView *upperImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0,416)];
if(pageCount == 72){
backBarButton.title=#"About Us";
if(img){
[newImageView setImage:[UIImage imageNamed:#"splashscreenhsd.png"]];}
else{
[newImageView setImage:[UIImage imageNamed:#"splashscreen.png"]];
}
[titleBarItem setTitle:#"0/72"];
[newView addSubview:newImageView];
pageCount=0;
} else {
pageCount++;
NSString *page=[NSString stringWithFormat:#"pagehsd%d.png",pageCount];
NSString *pageString=[NSString stringWithFormat:#"page%d.png",pageCount];
if(img){
[upperImageView setImage:[UIImage imageNamed:page]];
}
else{
[upperImageView setImage:[UIImage imageNamed:pageString]]; }
if(img){
[newImageView setImage:[UIImage imageNamed:#"backgroundhsd.png"]];}
else{
[newImageView setImage:[UIImage imageNamed:#"background.png"]];
}
[newImageView addSubview:upperImageView];
[newView addSubview:newImageView];
[titleBarItem setTitle:[NSString stringWithFormat:#"%d/72",pageCount]];
aboutUsButton.title=#"Back";
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[self view] cache:NO];
[[self view] addSubview:newView];
[UIView commitAnimations];
}
I am using this code and there is problem in this, when i continue to click the next button animation gets faster on every click and animation become invisible. Tell me the solution of this problem.
Play around animation duration (in seconds). The default value is 0.2
[UIView setAnimationDuration:1.0];