可拖动的 UITextField 在不应该的时候进入编辑状态

Posted

技术标签:

【中文标题】可拖动的 UITextField 在不应该的时候进入编辑状态【英文标题】:Draggable UITextField enters editing when it shouldn't 【发布时间】:2013-06-13 15:10:32 【问题描述】:

我正在为 iPad 开发一个 ios6 应用程序。我编写了 UITextField 的子类,它使用户能够围绕视图拖动、捏合和旋转字段。问题是如果你在没有旋转的情况下捏住字段,在完成手势后,它会进入编辑模式。这不应该发生,我添加了一些禁用它的行,但它并没有停止。这是我的代码:

- (BOOL) canBecomeFirstResponder
if (gesturing==YES) 
       return NO;
else  return YES;



 - (void) tapDetected:(UITapGestureRecognizer*) pinchRecognizer 

if (gesturing==NO) 
    [self becomeFirstResponder];




- (void) pinchDetected:(UIPinchGestureRecognizer*) pinchRecognizer 

CGFloat scale = pinchRecognizer.scale;
self.font = [self.font fontWithSize:self.font.pointSize*(scale)];


//self.transform = CGAffineTransformScale(self.transform, scale, scale);

[self sizeToFit];


pinchRecognizer.scale = 1.0;

if (pinchRecognizer.state == UIGestureRecognizerStateChanged) 
    gesturing =YES;   



if (pinchRecognizer.state == UIGestureRecognizerStateBegan) 
    gesturing =YES;



if (pinchRecognizer.state == UIGestureRecognizerStateEnded) 
    gesturing =NO;




 


- (void) panDetected:(UIPanGestureRecognizer *)panRecognizer 

CGPoint translation = [panRecognizer translationInView:self.superview];
CGPoint imageViewPosition = self.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;

self.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView: self.superview];



if (panRecognizer.state == UIGestureRecognizerStateChanged) 
    gesturing=YES;       



if (panRecognizer.state == UIGestureRecognizerStateEnded) 
    gesturing=NO;






  


 - (void) rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer  


CGFloat angle = rotationRecognizer.rotation;
self.transform = CGAffineTransformRotate(self.transform, angle);
rotationRecognizer.rotation = 0.0;

if (rotationRecognizer.state == UIGestureRecognizerStateChanged) 

    gesturing = YES;


if (rotationRecognizer.state == UIGestureRecognizerStateEnded) 

    gesturing = NO;


 


- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
return YES;
     

【问题讨论】:

【参考方案1】:

gesturing 的默认值是NO,如果你不将它初始化为YES。是否已调试并弄清楚当您尝试应用 pinch 时首先检测到哪个手势识别器? 您支持同时手势识别,因此当您应用pinch 时,您的手指触摸可能会被检测为pan。如果是这种情况,则 tapDetected: 方法中的 if(gesturing == NO) 条件得到满足,您的 textField 将成为第一响应者。

【讨论】:

以上是关于可拖动的 UITextField 在不应该的时候进入编辑状态的主要内容,如果未能解决你的问题,请参考以下文章

同时捏合、拖动和平移

如何在 iOS 11 的 UITextfield 中启用拖动

Java:如何在不使用 TransferHandler 的情况下拖动 JTable 行?

jquery 可拖动启用和禁用

圆形或圆形 Draggable/Droppables

如何在不输入的情况下检测 UITextField 光标移动?