如何使用iPhone SDK只拖动一张图像 [英] How to drag only one image with iPhone SDK

查看:52
本文介绍了如何使用iPhone SDK只拖动一张图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含两个图像的小应用程序,并且我只想使图像可拖动。
经过研究,我找到了以下解决方案:

I want to create a little app that takes two images and i want to make only the image over draggable. After research, i found this solution:

-(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;
    }

它可以工作,但问题是图像可从其中心拖动不想那样
所以我找到了另一个解决方案:

It works but the problem is that the image is draggable from its center and I don't want that. So I found another solution:

- (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开发的初学者,我不知道如何只能使图像在可拖动状态。

It works very well but when I add another image, all the images of the view are draggable at the same time. I'm a beginner with the iPhone development and I have no idea of how I can only make the image over draggable.

推荐答案

您可以在 touchesBegan:方法中使用 if 条件,请参阅此-

You can use if condition in the touchesBegan: method, See 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;
    }
}

这两个图像仅在您触摸时才会移动并移动它。希望我的教程对您有所帮助。

These two images will move only when you touch on it and move it. I hope my tutorials will help you.

这篇关于如何使用iPhone SDK只拖动一张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆