2012-02-08 54 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

+0

私はよく分かりませんが、問題は、UILableビューがタッチとやりとりしないということです。そのため、ビューの階層の上にイベントが渡されるため、viewControllerではtouchesBegan経由で取得してラベルを移動できます。しかし、UITextViewはtouchEventを使い果たしてしまいます。たとえば、キーボードがポップされたため、touchEventはスーパーに渡されません。あなたのケースではtouchesBeganメソッドは全く呼び出されません。 – Canopus

答えて

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

+0

@TWcode、はい、ちょうど[touch tapCount] == 2にチェックしてください。私の更新された回答を参照してください – beryllium

+0

あなたの助けをありがとう。 – TWcode

関連する問題