如何在iPhone上闪烁(或闪烁)光标?

我试图在UIKit中创build一个自定义的“闪烁的光标”,我已经尝试了如下所示,有2个函数,基本上保持相互调用,直到光标被隐藏。 但是,这导致了一个很好的无限recursion…出于某种原因,函数立即调用对方,而不是每半秒如预期。

如果“finished”参数不是YES(通过取消注释“if(!ok)”行),但是这导致根本没有animation…

更好的主意? 我错过了什么,有没有一个更简单的方法来做一个“闪烁的光标”?

- (void)onBlinkIn:(NSString *)animationID finished:(BOOL)ok context:(void *)ctx { if (cursorView.hidden) return; //if (!ok) return; [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(onBlinkOut:finished:context:)]; cursorView.textColor = [UIColor grayColor]; [UIView commitAnimations]; } - (void)onBlinkOut:(NSString *)animationID finished:(BOOL)ok context:(void *)ctx { if (cursorView.hidden) return; [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(onBlinkIn:finished:context:)]; cursorView.textColor = [UIColor clearColor]; [UIView commitAnimations]; } 

在委托上:

 - (void)blinkAnimation:(NSString *)animationId finished:(BOOL)finished target:(UIView *)target { if (shouldContinueBlinking) { [UIView beginAnimations:animationId context:target]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)]; if ([target alpha] == 1.0f) [target setAlpha:0.0f]; else [target setAlpha:1.0f]; [UIView commitAnimations]; } } 

并开始animation:

 shouldContinueBlinking = YES; [self blinkAnimation:@"blinkAnimation" finished:YES target:cursorView]; 

另外,确保你的类有一个shouldContinueBlinking实例variables

做核心animation的方式:

 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; [animation setFromValue:[NSNumber numberWithFloat:1.0]]; [animation setToValue:[NSNumber numberWithFloat:0.0]]; [animation setDuration:0.5f]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; [animation setAutoreverses:YES]; [animation setRepeatCount:20000]; [[view layer] addAnimation:animation forKey:@"opacity"]; 

哪里是你想要眨眼的UIView。 核心animation使这非常方便,因为它会自动为您倒转animation。 请记住,由于您指定的值适用于正向,因此您的完整持续时间是您在持续时间字段中设置的时间的两倍。 如果您希望整个animation在指定的时间内运行(向前,然后向后),请将时间分成两半。

Matt Long在Swift 3的回答:

 func addOpacityAnimation(view: UIView) { let key = "opacity" let animation = CABasicAnimation(keyPath: key) animation.fromValue = 1.0 animation.toValue = 0.0 animation.duration = 0.5 animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) animation.autoreverses = true animation.repeatCount = FLT_MAX view.layer.add(animation, forKey: key) } 

最有可能的是,你正在阻止主事件循环,因此,当你尝试只完成animation时阻止animation。

相反,设置一个计时器,在1/2秒后触发下一个animation。 这个定时器可以在前一个animation结束时重置,这样可以减less负载,使你的闪烁频率更加规律(但是你必须弄清楚什么是最合适的)。

请参阅NSTimer类的文档。

请注意,像这样的任何一种不断的animation将会耗尽电池。 不是一个巨大的,但是…仍然…