我正在尝试在UIKit中创建一个自定义的“闪烁光标”,我已尝试如下所示,有两个功能基本上一直保持相互调用,直到光标被隐藏。但是这导致了一个很好的无限递归...由于某种原因,这些函数会立即相互调用,而不是像预期的那样每半秒。
我尝试返回,如果'finished'参数不是YES(通过取消注释'if(!ok)'行),但这导致根本没有动画......
有什么好主意吗?我错过了什么,是否有更简单的方法来制作一个“闪烁的光标”?
- (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];
}
答案 0 :(得分:47)
以核心动画的方式做到:
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。核心动画使这非常方便,因为它会为你自动反转动画。请记住,您的完整持续时间是您在持续时间字段中设置的两倍,因为您指定的值适用于前进方向。如果您希望在指定的持续时间内运行(向前然后向后)整个动画,请将持续时间分成两半。
答案 1 :(得分:16)
代表:
- (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];
}
}
开始制作动画:
shouldContinueBlinking = YES;
[self blinkAnimation:@"blinkAnimation" finished:YES target:cursorView];
另外,请确保您的类具有shouldContinueBlinking实例变量
答案 2 :(得分:0)
最有可能的是,您阻止了主事件循环,因此,当您尝试仅在完成时设置动画时阻止动画。
相反,设置一个计时器,在1/2秒后触发,启动下一个动画。该计时器可以在上一个动画结束时重置,从而减少负载并使你的眨眼率更加规律(但你必须找出最合适的)。
请参阅NSTimer类的文档。
请注意,任何类似这样的常量动画都会耗尽电池电量。无论如何,这不是一个巨大的,但......还是......
答案 3 :(得分:0)
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"opacity";
animation.values = @[ @(0.0),@(0.0),@(1.0), @(1.0)];
animation.keyTimes = @[ @(0.001),@(0.49),@(0.50), @(1.0)];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.duration = 1.2;
animation.autoreverses = NO;
animation.repeatCount= HUGE;
[layer addAnimation:animation forKey:@"blink"];