xcode:如何从某些视图中禁用手势识别器?

时间:2012-07-23 15:22:06

标签: iphone xcode uigesturerecognizer

我有一个uiimageView的单击手势识别器,使用户可以在这个uiimageview上的任何地方点击并添加uiTextField。

有时候我的文本字段出现在我的uiimageview附近的问题。我希望textfield不会超过uiimageview框架。

这是我在ViewDidLoad中的代码:

// Create gesture recognizer
recognizer = 
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addText:)] autorelease];

// Set required taps and number of touches
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];

// Add the gesture to the view
[self.img addGestureRecognizer:recognizer];

并在方法addText中:

CGPoint point = [recognizer locationInView:[self view]];

UITextField * textFld = [[UITextField alloc]init];

[textFld setDelegate:self];

[textFld addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];

[textFld setFont:[UIFont fontWithName:@"System Bold" size:fontSize]];

CGRect frame ;    
frame.origin.x = point.x;
frame.origin.y = point.y; 
frame.size.width=300;
frame.size.height=80;

textFld.frame=frame;

[self.img addSubview:textFld];

[textFld release];

最后是imageMoved:withEvent:

- (IBAction) imageMoved:(id)sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;

}

1 个答案:

答案 0 :(得分:2)

您可以使用gestureRecognizer:shouldReceiveTouch:中的UIGestureRecognizerDelegate方法查看触摸发生的位置,并决定是否要对手势做出响应。如果触摸距离UIImageView的边缘太近,则返回NO,否则返回YES。或者只需查看touch.view,查看您的UIImageView是否发生了触摸。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch;