2011-01-29 4 views
0

2つの画像を取り込む小さなアプリを作成したいので、画像をドラッグ可能にしたいと思っています。iPhone SDKで画像を1つだけドラッグする方法

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

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

    image.alpha = 0.7; 

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

それは動作しますが、問題は、画像がその中心からドラッグ可能であると私はそれを望んでいないということです。 の研究の後、私はこの解決策を見つけました。 だから私は別の解決策が見つかりました:それは非常によく動作しますが、私は別の画像を追加するときに、ビューのすべての画像を同時にドラッグ可能です

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 
    CGPoint pt = [[touches anyObject] locationInView:self.view]; 
    startLocation = pt; 
    [[self view] bringSubviewToFront:self.view]; 

} 


- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Move relative to the original touch point 

    CGPoint pt = [[touches anyObject] locationInView:self.view]; 
    frame = [self.view frame]; 
    frame.origin.x += pt.x - startLocation.x; 
    frame.origin.y += pt.y - startLocation.y; 
    [self.view setFrame:frame]; 
} 

を。私はiPhone開発の初心者です。どのようにイメージをドラッグ可能にすることができるのか分かりません。

答えて

0

それを分解しましょう。 View ControllerにはメインのcontainerView(UIViewのオブジェクト)が必要です。その中には、DragableImageViewのオブジェクトが必要です。

DragableImageViewUIImageViewを継承し、その中にタッチコードを追加します。 (今はself.frame = positionが間違っています - 明らかに、すべての画像を一度に移動します - 私が正しく理解すれば、あなたのView Controllerにこれらのタッチを追加しているからです)。

まず、DragableImageViewのみを作成してテストします。次に、2つのDragableImageViewインスタンスを追加します。別の設定でそれらをテストしてください。containerViewのテストを打ち込んで、DragableImageViewをドラッグする必要があるか確認してください(ジグソーパズルの場合は、正面のものではなく背面のものを選ぶことができます)。あなたはそれに触れると、それを移動すると

1

は、単にあなたがtouchesBegan:方法でif条件を使用することができます

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

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesBegan:touches withEvent:event]; 
} 
3

これらの2つの方法を使用して、これらの2枚の画像がのみ移動しますthis-

-(void)ViewDidLoad 
{ 
    //Your First Image View 
    UIImageView *imageView1 = [UIImageView alloc]initWithFrame:CGRectMake(150.0, 100.0, 30.0, 30.0)]; 
    [imageView1 setImage:[UIImage imageNamed:@"anyImage.png"]]; 
    [imageView1 setUserInteractionEnabled:YES]; 
    [self.view addSubview:imageView1]; 

    //Your Second Image View 
    UIImageView *imageView2 = [UIImageView alloc]initWithFrame:CGRectMake(150.0, 200.0, 30.0, 30.0)]; 
    [imageView2 setImage:[UIImage imageNamed:@"anyImage.png"]]; 
    [imageView2 setUserInteractionEnabled:YES]; 
    [self.view addSubview:imageView2]; 
} 

//Now Use Touch begin methods with condition like this 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches]anyObject]; 
    if([touch view] == imageView1) 
    { 
     CGPoint pt = [[touches anyObject] locationInView:imageView1]; 
     startLocation = pt; 
    } 

    if([touch view] == imageView2) 
    { 
     CGPoint pt = [[touches anyObject] locationInView:imageView2]; 
     startLocation = pt; 
    } 
} 

//Use same thing with touch move methods... 
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches]anyObject]; 
    if([touch view] == imageView1) 
    { 
     CGPoint pt = [[touches anyObject] previousLocationInView:imageView1]; 
     CGFloat dx = pt.x - startLocation.x; 
     CGFloat dy = pt.y - startLocation.y; 
     CGPoint newCenter = CGPointMake(imageView1.center.x + dx, imageView1.center.y + dy); 
     imageView1.center = newCenter; 
    } 

    if([touch view] == imageView2) 
    { 
     CGPoint pt = [[touches anyObject] previousLocationInView:imageView2]; 
     CGFloat dx = pt.x - startLocation.x; 
     CGFloat dy = pt.y - startLocation.y; 
     CGPoint newCenter = CGPointMake(imageView2.center.x + dx, imageView2.center.y + dy); 
     imageView2.center = newCenter; 
    } 
} 

を参照してください。 。私はチュートリアルがあなたに役立つことを願っています。

+0

startLocationとは何ですか? –

+0

** startLocation **は、** CGPoint **タイプのグローバル変数です。 – TheTiger

関連する問題