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

How to make a blinking (or flashing) cursor on iphone?(如何在 iPhone 上制作闪烁(或闪烁)的光标?)

本文介绍了如何在 iPhone 上制作闪烁(或闪烁)的光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a custom "blinking cursor" in UIKit, I've tried as shown below, having 2 functions that basically keep calling each other until the cursor is hidden. But this leads to a nice infinite recursion... for some reason the functions call each other right away, not each half-second as expected.

如果 'finished' 参数不是 YES,我尝试返回(通过取消注释 'if (!ok)' 行),但这根本不会导致动画...

I tried returning if the 'finished' parameter is not YES (by uncommenting the 'if (!ok)' line), but that leads to no animation at all...

有更好的主意吗?我错过了什么吗,有没有更简单的方法来制作闪烁光标"?

Any better idea? Did I miss something, is there a much-easier way to make a "blinking cursor"?

- (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];
    }
}

然后开始动画:

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

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

Also, ensure your class has a shouldContinueBlinking instance variable

这篇关于如何在 iPhone 上制作闪烁(或闪烁)的光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 iPhone 上制作闪烁(或闪烁)的光标?

基础教程推荐