当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Making a UIImageView opacity (alpha) change randomly

问题描述:

I'm new to the world of coding and I'm trying to simulate a flickering light effect in my project, to do so I have a background image(image1) and another image (image2) on top of it.

I would like to make the opacity of image 2 change randomly, but I can't figure out how to make the opacity flicker randomly an infinite number of time, I tried with a UIView animation without success.

[self.view addSubview: image1];

[image1 addSubview: image2];

[UIView animateWithDuration:0.2f

delay:1.0f

options: UIViewAnimationOptionCurveEaseIn

animations:^(void) {

image2.alpha = 0.0

}

completion:^(BOOL finished){

image2.alpha = 1.0

}];

Any help would be greatly appreciated.

Thank you

网友答案:

Use the following UIViewAnimationOptions to repeat the animation and autoreverse:

UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat

To randomize the flickering you can randomize either the duration or delay using the arc4random function:

CGFloat duration = arc4random() % 3 + 1

Example:

[UIView animateWithDuration: duration
                  delay:1.0f
                options:  UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
             animations:^(void) {
                image2.alpha = 0.0
               }
             completion:^(BOOL finished){
              }];

This will repeat the animation with the initial randomized value (it will not randomize each time).

If you want to repeat it every time with a random value as you mentioned you will need to remove the UIViewKeyframeAnimationOptionRepeat and UIViewKeyframeAnimationOptionAutoreverse and do a little trick. Create a function to generate your animation (generate random variables, etc) and instead on the completion block call the function to generate random values and run the animation again.

Example:

-(void)generateFlicker
{

[UIView animateWithDuration:0.5
                      delay:0.0f
                    options:nil
                 animations:^(void) {
                     self.view.alpha = (arc4random() % 100)/100.0f; //generates random number 0.0 to 1.0
                 }
                 completion:^(BOOL finished){
                     [self generateFlicker];
                 }];
   }

What this does it generates a random number from 0.0 to 1.0 and sets the alpha to that, completes and then calls itself and sets the alpha to the new generated value.

If you want to generate a random delay, duration or other values you can generate random variables before the animation as well as the function will be called each time it runs.

网友答案:

To randomize alpha of the view you have to generate random float between 0.o to 1.0:

#define ARC4RANDOM_MAX      0x100000000

float random_alpha = ((float)arc4random() / ARC4RANDOM_MAX); //use this within animation loop.

A bit more details here

Example:

[UIView animateWithDuration: 1.0
                  delay:1.0f
                options:  UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
             animations:^(void) {
                float random_alpha = ((float)arc4random() / ARC4RANDOM_MAX);
                some_image.alpha = random_alpha;
               }
             completion:^(BOOL finished){
              }];
分享给朋友:
您可能感兴趣的文章:
随机阅读: