0

我试图实现的方法是将按钮设置为 0.5 alpha,然后在三秒内返回 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;

}
4

3 回答 3

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
}];

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

于 2013-05-28T05:34:50.430 回答
0

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

于 2013-05-28T05:12:39.770 回答
0

好吧,我已经使用这个网站好几年了,我应该得到一个很好的全面回答来回馈。

我建立了一个项目并完成了您想要的功能。一些注意事项:

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

-我决定从 1->.5->1 淡出,因为 .5 alpha 让我很烦。如果你想扭转它,应该不难改变它。

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

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

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

.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
于 2013-05-28T10:55:04.613 回答