- 新的 toast 来了, 如果当前的 toast 还没有结束, 需要停止当前的延迟计时器, 重新开启一个新的计时器.
- 新的 toast 来了, 会因为动画地显示而闪一下, 可以根据 toast 是否显示, 来确定是否需要添加动画.
1 #pragma mark 隐藏视图 2 - (void)hideSelfViewWithDelay{ 3 [self performSelector:@selector(hideSelfView) withObject:nil afterDelay:2]; 4 } 5 6 - (void)cancelHideSelfViewWithDelay{ 7 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideSelfView) object:nil]; 8 } 9 10 11 /** 12 更新 toast 提示, 重新延迟2秒显示 13 */ 14 - (void)restartHideSelfViewWithDelay{ 15 [self cancelHideSelfViewWithDelay]; 16 [self hideSelfViewWithDelay]; 17 } 18 19 20 - (void)hideSelfView{ 21 self.alpha = 1; 22 [UIView animateWithDuration:0.25 animations:^{ 23 self.alpha = 0.1; 24 } completion:^(BOOL finished) { 25 self.hidden = YES; 26 }]; 27 } 28 29 30 /** 31 如果已经显示, 就不添加动画 32 */ 33 - (void)showSelfView{ 34 if (self.hidden == YES) { 35 self.hidden = NO; 36 self.alpha = 0.1; 37 [UIView animateWithDuration:0.25 animations:^{ 38 self.alpha = 1; 39 }]; 40 } 41 42 [self restartHideSelfViewWithDelay]; 43 } 44 45 - (void)dealloc{ 46 [self cancelHideSelfViewWithDelay]; 47 }
转载于:https://www.cnblogs.com/lz465350/p/9209877.html