2010-12-02 9 views
1

iPhone SDKでオブジェクトをドラッグする際に問題があります。私のUIImageViewコンセントは赤で、ドラッグが機能しない理由はわかりません!しかし、もし行を削除したときに私のコードは、ここでは、正常に動作コードです:あなたはUIViewのサブクラスのtouchesBeganを上書きする場合特定のオブジェクトをドラッグ

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 


    UITouch *touch = [[event allTouches]anyObject]; 

    if ([touch view] == red) { 
    CGPoint location = [touch locationInView:self.view]; 
    red.center = location; 

    } 
} 


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

     [self touchesBegan:touches withEvent:event]; 
    } 
+1

CGPoint – Momi

答えて

1

、そのUIViewのに配信のみタッチが処理されます。

サブビューでは、UserInteractionEnabledをTRUEに設定する必要があります。そして、あなたのSuperviewにタッチ処理を委譲することができるあなたのサブビューのサブクラスを作成します。

それは複雑、ちょうど を使用して聞こえる場合 - (UIViewの*)hitTest:(するCGPoint)ポイントwithEvent:(たUIEvent *)イベント

+0

ありがとう、お母さんの権利の "touchesMoved"にあなたのオブジェクトの位置を記述する必要があります! 、私はこのオブジェクトを画面の周りにドラッグしたい –

+0

ええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええ、 –

0

をこんにちは、私はコードの下でそれをやりました。特定の水平方向と垂直方向にimageEndをドラッグする必要がありますので、以下のようにバインドします。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView: touch.view]; 
location.x=510.0; 
[imgEndPoint setUserInteractionEnabled:TRUE]; 

if ([touch view] == imgEndPoint) { 
    if (location.y < 710.0 && location.y > 75.0) {//image move between this coordinate 
     imgEndPoint.center = location; 
    } 
} 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches] anyObject]; 

if ([touch view] == imgEndPoint) { 

    CGPoint location = [touch locationInView: self.view]; 
    location.x=510.0; 
    if (location.y < 710.0 && location.y > 75.0) { 
    imgEndPoint.center = location; 
     [self getPixcel]; 
    } 
    [imgEndPoint setUserInteractionEnabled:TRUE]; 
} 
} 
関連する問題