2012-01-25 10 views
0

私はiPadアプリのために現時点でインタラクティブなアナグラム機能を構築していますが、選択した項目をスムーズにフォローするためにtouchmoveイベントを使用しようとしていますが、理由。iPhoneのドラッグアンドドロップの奇妙なジャンプムーブ

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]]; 

    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) { 
     NSLog(@"start moving"); 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]]; 
    CGPoint location = [touch locationInView:touch.view]; 
    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) { 
     NSLog(@"touch view centre (x,y) - (%f,%f)",touch.view.center.x,touch.view.center.y); 
     NSLog(@"location (x,y) - (%f,%f)",location.x,location.y); 
     touch.view.center = location; 
    } 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]]; 

    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]){ 
     NSLog(@"Now stop moving!"); 
    } 
} 

上記は私の3タッチの方法であり、正常に動作するはずです。 touchesMovedメソッドでは、移動対象のオブジェクトが特定のクラス(同じプロジェクトに対して作成したカスタムクラス)かどうかを確認しています。なぜこのようなことが起こっているのか誰にも考えていますか?それはシミュレータのカーソルに追従していますが、カーソルの後にくる前に下隅にスナップしているように見えるので、基本的に画面全体にジャンプしています。

アイデア?

答えて

1

私は、あなたが触れているサブビューを基準にしてタッチで作業していると考えています。

[touch locationInView:[touch.view superview]]; 

あなたのカスタムビューの親を参照する場所が表示されます。あなたは、変位を見て、あなたのビューに

if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) { 
     UITouch * touch = [touches anyObject]; 
     CGPoint previous = [touch previousLocationInView:[touch.view superview]]; 
     CGPoint current = [touch locationInView:[touch.view superview]]; 
     CGPoint displacement = CGPointMake(current.x - previous.x, current.y - previous.y); 

     CGPoint center = touch.view.center; 
     center.x += displacement.x; 
     center.y += displacement.y; 
     touch.view.center = center; 
} 
+1

を同じ変位を適用することができtouchesMoved方法で

は、感謝を御馳走を働きました!そのコードはすべてのプロジェクトでリサイクルされます:) –