如何用iPhone SDK只拖动一个图像

我想创build一个需要两个图像的小应用程序,并且我只想使可拖动的图像。 经过研究,我发现这个解决scheme:

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

它的工作原理,但问题是,图像是从中心拖动,我不希望这一点。 所以我find另一个解决scheme

 - (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开发的初学者,我不知道如何才能使图像过度拖拽。

iftouchesBegan:方法中使用if条件,请参阅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; } } 

只有当你触摸并移动它时,这两个图像才会移动。 我希望我的教程能帮助你。

只需使用这两种方法

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

我们来分解一下。 你应该有一个主视图控制器的containerView(UIView的对象)。 在那里,你应该有DragableImageView对象。

DragableImageViewinheritance了UIImageView ,在其中添加了触摸代码。 (现在你正在做self.frame = position这是错误的self.frame = position – 显然它会一次移动所有的图像 – 因为如果我理解正确,你在视图控制器中添加这些触摸)。

首先创buildDragableImageView ,然后进行testing。 然后添加两个DragableImageView实例。 testing他们在不同的configuration..你可能想在containerView查看命中testing,看看哪些DragableImageView应该被拖动(例如,如果你有拼图碎片,你可能想select一个在后面,而不是前面)。