使 UIImageView 不透明度(alpha)随机变化
Making a UIImageView opacity (alpha) change randomly
我是编码领域的新手,我想在我的项目中模拟闪烁的灯光效果,为此我有一个背景图像 (image1) 和另一个图像 (image2) 在它上面.
我想使图像 2 的不透明度随机变化,但我不知道如何使不透明度随机闪烁无限次,我尝试使用 UIView 动画但没有成功。
[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
}];
如有任何帮助,我们将不胜感激。
谢谢
使用以下 UIViewAnimationOptions 重复动画和自动反转:
UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
要随机化闪烁,您可以使用 arc4random 函数随机化持续时间或延迟:
CGFloat duration = arc4random() % 3 + 1
示例:
[UIView animateWithDuration: duration
delay:1.0f
options: UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
animations:^(void) {
image2.alpha = 0.0
}
completion:^(BOOL finished){
}];
这将使用初始随机值重复动画(不会每次都随机化)。
如果你想每次都用你提到的随机值重复它,你需要删除 UIViewKeyframeAnimationOptionRepeat
和 UIViewKeyframeAnimationOptionAutoreverse
并做一些小技巧。创建一个函数来生成你的动画(生成随机变量等),而不是在完成块上调用函数来生成随机值并再次 运行 动画。
示例:
-(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];
}];
}
这是做什么的,它生成一个从 0.0 到 1.0 的随机数并将 alpha 设置为其,完成然后调用自身并将 alpha 设置为新生成的值。
如果您想生成随机延迟、持续时间或其他值,您可以在动画之前生成随机变量,并且每次 运行s 都会调用该函数。
要随机化视图的 alpha
,您必须在 0.o 到 1.0:
之间生成随机浮点数
#define ARC4RANDOM_MAX 0x100000000
float random_alpha = ((float)arc4random() / ARC4RANDOM_MAX); //use this within animation loop.
更多细节here
示例:
[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){
}];
我是编码领域的新手,我想在我的项目中模拟闪烁的灯光效果,为此我有一个背景图像 (image1) 和另一个图像 (image2) 在它上面.
我想使图像 2 的不透明度随机变化,但我不知道如何使不透明度随机闪烁无限次,我尝试使用 UIView 动画但没有成功。
[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
}];
如有任何帮助,我们将不胜感激。
谢谢
使用以下 UIViewAnimationOptions 重复动画和自动反转:
UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
要随机化闪烁,您可以使用 arc4random 函数随机化持续时间或延迟:
CGFloat duration = arc4random() % 3 + 1
示例:
[UIView animateWithDuration: duration
delay:1.0f
options: UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
animations:^(void) {
image2.alpha = 0.0
}
completion:^(BOOL finished){
}];
这将使用初始随机值重复动画(不会每次都随机化)。
如果你想每次都用你提到的随机值重复它,你需要删除 UIViewKeyframeAnimationOptionRepeat
和 UIViewKeyframeAnimationOptionAutoreverse
并做一些小技巧。创建一个函数来生成你的动画(生成随机变量等),而不是在完成块上调用函数来生成随机值并再次 运行 动画。
示例:
-(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];
}];
}
这是做什么的,它生成一个从 0.0 到 1.0 的随机数并将 alpha 设置为其,完成然后调用自身并将 alpha 设置为新生成的值。
如果您想生成随机延迟、持续时间或其他值,您可以在动画之前生成随机变量,并且每次 运行s 都会调用该函数。
要随机化视图的 alpha
,您必须在 0.o 到 1.0:
#define ARC4RANDOM_MAX 0x100000000
float random_alpha = ((float)arc4random() / ARC4RANDOM_MAX); //use this within animation loop.
更多细节here
示例:
[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){
}];