一旦用户点击特定按钮,我需要对图像执行闪烁效果。但我的代码不起作用:
- (void)next
{
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
NSLog(@"Done"); // <- USED AS COUNTER
[image setAlpha:0];
[image setAlpha:0.5];
} completion:nil];
}
我的image
将其alpha值更改为0.5并停止。此外,我的控制台只显示DONE
一次。我错了什么?
答案 0 :(得分:0)
我刚刚创建了一个测试项目,这眨了眨眼......
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong)UIView *blinker;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.blinker = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.blinker.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.blinker];
[self next];
}
- (void)next
{
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
NSLog(@"Done"); // <- USED AS COUNTER
[self.blinker setAlpha:0];
[self.blinker setAlpha:0.5];
} completion:nil];
}
除了您希望日志继续在控制台中打印出来之外。所以我怀疑你正在为你的形象做些什么。你不是在称自己。所以我猜它是一个实例变量(ivar),你正在改变它指向代码中其他地方的东西。
如果您需要一个计数器来熄灭每一次眨眼,您可能需要考虑这样的事情......
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong)UIView *blinker;
@property (nonatomic,)NSInteger counter;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.blinker = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.blinker.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.blinker];
[self next];
}
- (void)next
{
[UIView animateWithDuration:1.0 animations:^{
self.counter++;
NSLog(@"Counter: %@", @(self.counter));
self.blinker.alpha = 0.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
self.blinker.alpha = 1.0;
} completion:^(BOOL finished) {
//check to see if you want to continue blinking if so call next again
[self next];
}];
}];
}
我没有任何关于你为什么要数数的背景,但希望这有帮助。