在用户点击屏幕的任何位置创建UITextField

时间:2012-10-31 05:37:01

标签: ios ipad uitextfield

我正在制作一款iPad应用程序,能够识别用户在视图中的位置并在该位置创建UITextField。编程的最有效方法是什么?它适用于多个文本字段吗?

3 个答案:

答案 0 :(得分:1)

我认为,就UI而言,您的想法听起来有点不稳定,但您可能想要使用UITapGestureRecognizer。代码看起来像这样:

// In viewDidLoad
UITapGestureRecognizer *tapRecognizer = [UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedInView:)];
[self.view addGestureRecognizer:tapRecognizer];


- (void)tappedInView:(UIGestureRecognizer *)gestureRecognizer
{
     CGPoint pointForTextField = [gestureRecognizer locationInView:gestureRecognizer.view];
     // Add the UITextField to your view. You could use the pointForTextField as either the origin or center of the text field
    UITextField *textField = [UITextField alloc] initWithFrame:CGRectMake(pointForTextField.x,pointForTextField.y,100,44)];
    [self.view addSubview:textField];
}

答案 1 :(得分:0)

//text field move where you touch in screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
    point = [myTouch locationInView:self.view];
    NSLog(@"%f,%f",point.x, point.y);
    yourTextField.frame = CGRectMake(point.x, point.y, width,height);
}

答案 2 :(得分:0)

这样做:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
   if(locationPoint)
   {
     //add UITextField
     yourTextField.frame = CGRectMake(locationPoint.x, locationPoint.y, 40,40); //change height and width according
   }

}