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

Zor*_*mic 18 objective-c uikit ios

我正在尝试在UIKit中创建一个自定义的"闪烁光标",我尝试过如下所示,有2个功能基本上一直保持相互调用,直到光标被隐藏.但这导致了一个很好的无限递归...由于某种原因,这些函数会立即相互调用,而不是像预期的那样每半秒.

我尝试返回如果'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];
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ong 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"];
Run Code Online (Sandbox Code Playgroud)

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

  • 而不是20000你应该使用HUGE_VALF.然后它永远重复 (2认同)
  • @ josema.vitaminew我只想重复20K次.永远会太过分了.;-) (2认同)

rpe*_*ich 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];
    }
}
Run Code Online (Sandbox Code Playgroud)

并开始动画:

shouldContinueBlinking = YES;
[self blinkAnimation:@"blinkAnimation" finished:YES target:cursorView];
Run Code Online (Sandbox Code Playgroud)

另外,确保您的类具有shouldContinueBlinking实例变量

  • 此外,设置alpha而不是textColor将允许GPU完成所有绘图并提高性能.类似地,使cursorView成为UIView并设置backgroundColor将比UILabel使用更少的RAM和CPU(并且比UITextView或UITextField少得多) (2认同)

Bar*_*uik 5

Matt Long 在 Swift 4 中的回答:

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 = Float.greatestFiniteMagnitude
    view.layer.add(animation, forKey: key)
}
Run Code Online (Sandbox Code Playgroud)