0

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

4

3 回答 3

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];
}
于 2012-10-31T05:53:27.903 回答
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);
}
于 2012-10-31T06:02:36.040 回答
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
   }

}
于 2012-10-31T06:03:35.717 回答