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你想要眨眼.核心动画使这非常方便,因为它会为你自动反转动画.请记住,您的完整持续时间是您在持续时间字段中设置的两倍,因为您指定的值适用于前进方向.如果您希望在指定的持续时间内运行(向前然后向后)整个动画,请将持续时间分成两半.
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实例变量
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)
| 归档时间: |
|
| 查看次数: |
12422 次 |
| 最近记录: |