2012-10-31 67 views

回答

1

我認爲你的想法聽起來有點不可思議,但你可能想要使用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]; 
} 
+0

將這項工作,爲多個文本字段? – Chiapuz79

+0

是的,每次點擊你的人只會添加一個新的文本框。 – MaxGabriel

+0

添加代碼以顯示如何添加文本字段。順便說一句,我強烈建議這個方法通過'touchesBegan'。 – MaxGabriel

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); 
} 
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 
    } 

} 
相關問題