简体   繁体   English

动画时 UITextField 无法成为第一响应者

[英]UITextField cannot become first responder while animating

At the moment, I am creating a feature that animates a UITextfield from the bottom of the screen to a given point using a UIView animation block.目前,我正在创建一个功能,该功能使用 UIView 动画块将 UITextfield 从屏幕底部动画到给定点。

For some reason while the UITextfield animates upwards, it is unresponsive to touch events and will not become the first responder until after it calls completion.出于某种原因,当 UITextfield 向上动画时,它对触摸事件没有响应,并且在调用完成之前不会成为第一响应者。

I have created a sample project below to show the issue.我在下面创建了一个示例项目来显示问题。

https://github.com/AdamBCo/Textfield-Animation-Issue https://github.com/AdamBCo/Textfield-Animation-Issue

As you can see in the example, UIViewAnimationOptionAllowUserInteraction is set in the UViewAnimation block.正如您在示例中看到的,UIViewAnimationOptionAllowUserInteraction 在 UViewAnimation 块中设置。

Any help on this issue would be greatly appreciated!对这个问题的任何帮助将不胜感激!

I was able to solve the problem by implementing the following solution:我能够通过实施以下解决方案来解决问题:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if ([self.textField.layer.presentationLayer hitTest:touchLocation])
    {
        [self.textField becomeFirstResponder];
    }
}

I have since updated the sample code online to show the animation of the presentation layer, the ending destination of the model layer, and a way to see if the clicks are inside of the presentation layer.此后我在线更新了示例代码,以显示表示层的动画、模型层的结束目的地以及查看点击是否在表示层内部的方法。

This is actually the intended action.这实际上是预期的操作。 What you are seeing is the CALayer animating therefore it won't respond to a touch until it's "landed".您所看到的是 CALayer 动画,因此它在“着陆”之前不会响应触摸。 What you can do is add a touch recognizer then test your hit points to begin responding on a certain object.您可以做的是添加一个触摸识别器,然后测试您的命中点以开始对某个对象做出响应。 I have to ask though, what UI/UX scenario would this be good practice?不过,我不得不问,什么 UI/UX 场景是好的做法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM