Iphone 拖动特定的UIImageView

Iphone 拖动特定的UIImageView,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,在我的代码中,我有一个对象数组,这些对象是UIImageView,其中包含UIImages 我在-(void)touchesMoved:(NSSet*)toucheevent:(UIEvent*)event中使用for循环逐个移动数组中的所有对象 一旦它们被移动到主视图上的不同位置,我希望能够抓住这些对象中的任何一个,并将它们移动到其他地方 我不知道怎样才能最好地做到这一点。是否有一种方法可以使用touchesbegin,以便仅拖动我单击的项目 基本上,现在有五个UIVIEVIEW,每一个都有U

在我的代码中,我有一个对象数组,这些对象是UIImageView,其中包含UIImages

我在
-(void)touchesMoved:(NSSet*)toucheevent:(UIEvent*)event
中使用for循环逐个移动数组中的所有对象

一旦它们被移动到主视图上的不同位置,我希望能够抓住这些对象中的任何一个,并将它们移动到其他地方

我不知道怎样才能最好地做到这一点。是否有一种方法可以使用touchesbegin,以便仅拖动我单击的项目


基本上,现在有五个UIVIEVIEW,每一个都有UIIMAGE,我正试图达到一个点,当我点击其中任何一个时,它们都可以移动。

< P>你可能想考虑向你想移动的视图添加手势识别器。通过这种方式,您可以很容易地判断它们何时被点击,并且您可以使用触摸移动它们……

在我的可拖动视图中,我使用
UIPangestureRecognitizer
做了类似的事情,取得了巨大的成功。要连接它,它可能看起来像这样:

@implementation DraggableView

-(id)initWithFrame:(CGRect)frame
{
   ...
   ...
   UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)] autorelease];
   [self addGestureRecognizer:panGesture];
   ..
   return self;
}
@end
pan:
方法中,您可以检查手势的状态,甚至捕获手势的位置并使用它重新定位视图。下面是一个大致的例子,说明它可能是什么样子:

-(void)pan:(UIPanGestureRecognizer *)gesture
{

   if (gesture.state==UIGestureRecognizerStateChanged)
   {
      //We're moving!
      UIView *aRootView = <perhaps your root view controller's view>;
      CGPoint currentOrigin = [gesture translationInView:aRootView];
      self.frame = CGRectMake(currentOrigin.x,currentOrigin.y,self.frame.size.width,self.frame.size.hight);
      ...
      ...
   }

}
-(void)平移:(UIPangestureRecognitor*)手势
{
if(手势.state==UIGestureRecognitzerStateChanged)
{
//我们要走了!

UIView*aRootView=编辑:John Muchow在博客上发布了这篇文章。你应该能够根据自己的目的进行调整

//UIDraggableImageView.h
#import <UIKit/UIKit.h>

@interface UIDraggableImageView : UIImageView
{
    CGPoint currentPoint;
}

@end

//UIDraggableImageView.m
#import "UIDraggableImageView.h"

@implementation UIDraggableImageView

- (id)initWithImage:(UIImage *)image
{
    if (self = [super initWithImage:image]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}

-(void)dealloc {
    [super dealloc];
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // When a touch starts, get the current location in the view
    currentPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Get active location upon move
    CGPoint activePoint = [[touches anyObject] locationInView:self];

    // Determine new point based on where the touch is now located
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                   self.center.y + (activePoint.y - currentPoint.y));

    //--------------------------------------------------------
    // Make sure we stay within the bounds of the parent view
    //--------------------------------------------------------
    float midPointX = CGRectGetMidX(self.bounds);
    // If too far right...
    if (newPoint.x > self.superview.bounds.size.width  - midPointX)
        newPoint.x = self.superview.bounds.size.width - midPointX;
    else if (newPoint.x < midPointX)  // If too far left...
        newPoint.x = midPointX;

    float midPointY = CGRectGetMidY(self.bounds);
    // If too far down...
    if (newPoint.y > self.superview.bounds.size.height  - midPointY)
        newPoint.y = self.superview.bounds.size.height - midPointY;
        else if (newPoint.y < midPointY)  // If too far up...
        newPoint.y = midPointY;

    // Set new center location
    self.center = newPoint;
}

@end
//UIDraggableImageView.h
#进口
@接口UIDRagableImageView:UIImageView
{
CGPoint-currentPoint;
}
@结束
//UIDRagableImageView.m
#导入“UIDRagableImageView.h”
@UIDRagableImageView的实现
-(id)initWithImage:(UIImage*)图像
{
if(self=[super initWithImage:image]){
self.userInteractionEnabled=是;
}
回归自我;
}
-(无效)解除锁定{
[super dealoc];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
//当触摸开始时,获取视图中的当前位置
currentPoint=[[Touchs anyObject]locationInView:self];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
//移动时获得活动位置
CGPoint activePoint=[[toucheanyobject]locationInView:self];
//根据触摸屏现在所在的位置确定新点
CGPoint newPoint=CGPointMake(self.center.x+(activePoint.x-currentPoint.x),
self.center.y+(activePoint.y-currentPoint.y));
//--------------------------------------------------------
//确保我们保持在父视图的范围内
//--------------------------------------------------------
float midPointX=CGRectGetMidX(self.bounds);
//如果太对了。。。
if(newPoint.x>self.superview.bounds.size.width-midPointX)
newPoint.x=self.superview.bounds.size.width-midPointX;
else if(newPoint.xself.superview.bounds.size.height-midPointY)
newPoint.y=self.superview.bounds.size.height-midPointY;
else if(newPoint.y
在按钮上单击将图像从摄影机滚动添加到imageview。图像可以放大和缩小,并应将该图像拖动到视图中的任何位置。

请一步一步地回答,并将您已经尝试过的代码添加到您的答案中,以使其成为有用的答案