2012-02-08 22 views
1

我已將它設置在可以移動多個標籤的位置。移動UITextfield時遇到問題。我確保用戶交互和多點觸控已啓用。如何拖動和移動UITextFields?

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:self.view]; 

if ([touch view] == tex1) { 
    tex1.center = location; 
+0

在哪個班你定義這個方法? – omz 2012-02-08 19:23:26

+0

我不太清楚,但我認爲問題在於UILable視圖不與觸摸交互,因此它將事件向上傳遞給視圖,因此在您的viewController中,您可以通過touchesBegan獲取它並移動標籤。但是UITextView耗盡了touchEvent,例如,它彈出鍵盤,所以touchEvent不會傳遞給超級。所以在你的情況下,touchesBegan方法根本不會被調用。 – Canopus 2012-02-08 19:55:53

回答

6

試圖使這一招,它爲我工作

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIView *clearView = [[UIView alloc] initWithFrame:tex1.frame]; 
    [clearView setTag:101]; 
    [self.view addSubview:clearView]; 
    [clearView release]; 
} 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 

    if ([[touch view] tag] == 101 && [touch tapCount] == 2) { 
     [tex1 becomeFirstResponder]; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:self.view]; 

    if ([[touch view] tag] == 101) { 
     tex1.center = location; 
     [[touch view] setCenter:location]; 
    } 
} 
+0

得到它的工作!雙擊文本字段時有沒有辦法調用鍵盤?我想點擊移動並雙擊編輯。 – TWcode 2012-02-09 03:07:14

+0

@TWcode,是的,只需檢查[觸摸tapCount] == 2,看我更新的答案 – beryllium 2012-02-09 08:18:29

+0

感謝您的幫助。 – TWcode 2012-02-09 11:43:52