按钮列表随机点亮

时间:2013-05-28 04:24:10

标签: objective-c xcode uibutton uianimation

我试图实现的方法是将按钮设置为0.5 alpha,然后在3秒的时间内将其设置为1。在四个按钮上执行5次后,代码块就完成了。我正在努力找到一种可以实现这一目标的方法,因为现在下面的块将是一个无限循环,当我希望它只能执行一次。

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;

}

3 个答案:

答案 0 :(得分:1)

当您将按钮的alpha设置为0.5然后立即设置为1时,按钮的alpha将不会设置动画。你可以了解这个片段

[UIView animateWithDuration:0.3 animations:^{
    [your_btn setAlpha:0.5];
} completion:^(BOOL finished) {
    if(finished)
        [self performSelector:@selector(revertAlphaToOne) withObject:nil afterDelay:0.5];
}];

revertAlphaToOne方法中,您可以将按钮的alpha恢复为1

 [UIView animateWithDuration:0.3 animations:^{
    [your_btn setAlpha:1.0];
} completion:nil
}];

根据您的喜好调整时间变量和/或在第一个块的完成块中调用第二个片段。

答案 1 :(得分:0)

问题在于,在案例1和案例2中,您将alpha值设置为0.5,然后立即返回1。因此,当动画开始时,它只是1并将保持不变。只需删除第二个分配,动画就会将alpha更改为0.5。 PS:如前所述,生成的随机数是0到4,而不是1到5。

答案 2 :(得分:0)

好吧,我已经使用这个网站好几年了,我应该回复一个很好的答案。

我构建了a project并完成了您想要的功能。一些说明:

- 关键是递归!您需要为淡出设置设置动画(使用我提供的功能),并在完成后使用相同的功能设置淡入淡出动画。完成后,运动递归并准备调用自己(beginAnimations),直到所有的整数= 5。

- 我决定从1-> .5-> 1淡出,因为.5 alpha让我讨厌。如果你想让它逆转,不应该很难切换它。

- 要确保没有按钮消失超过五次,您需要声明并增加与每个按钮对应的int。

- 使用arc4random(),而不是随机()。随机用于调试,因为每次都会得到相同的结果。

- 保持你的开关盒直;很难理解你想要在他们之间做些什么。在此注释中,使用break和默认语句!祝你在应用程序中使用所有这些好运。

的.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