我有一个自定义的UIView,它的行为是这样的:在我从nib加载它并将它添加到我的视图层次结构后,它首先几乎是透明的(alpha = 0.1),当我点击它时,它变得不透明(alpha = 1.0),一段时间后,它会自动变得几乎透明(alpha = 0.1).
自定义视图中的代码是这样的,它就像我上面描述的那样工作:
- (void)awakeFromNib {
[self setup];
}
- (void)setup {
self.alpha = 0.1f;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapped:)]];
}
- (void)tapped:(UITapGestureRecognizer *)tapRecognizer {
if (self.alpha == 1.0) {
[self hideSelf];
} else {
[UIView animateWithDuration:0.5
animations:^{ self.alpha = 1.0f; }
completion:^(BOOL finished) {
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer timerWithTimeInterval:3
target:self
selector:@selector(hideSelf)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:self.timer
forMode:NSDefaultRunLoopMode];
}];
}
}
- (void)hideSelf {
[UIView animateWithDuration:0.5
animations:^{ self.alpha = …Run Code Online (Sandbox Code Playgroud)