如何在iPhone中管理多个UIImageView

如何在iPhone中管理多个UIImageView,iphone,Iphone,我想在每次点击主视图时动态创建UIImageView,还想通过手指移动所有创建的UIImageView。我已经成功地在每次触摸时动态创建我的代码如下: -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint pt = [touch locationInView:self.view]; imgView

我想在每次点击主视图时动态创建
UIImageView
,还想通过手指移动所有创建的
UIImageView
。我已经成功地在每次触摸时动态创建我的代码如下:

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

    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self.view];

    imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(pt.x,pt.y, 100, 100)] autorelease];
    imgFilepath = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"png"];
    img = [[UIImage alloc] initWithContentsOfFile:imgFilepath];
    imgView.tag=i;  
    [imgView setImage:img];
    [img release];
    [self.view addSubview:imgView];
}  
现在我想用手指移动任何动态创建的
UIImageView

谢谢。

触摸中开始
检测您触摸过的
图像视图
。在
touchsmoved
中,通过将选定的
imageView
的中心设置为移动的触点来移动该视图。

触摸开始时
检测您已触摸的
imageView
。在
touchsmoved
中,通过将选定的
imageView的中心设置为移动的接触点来移动它。

在您创建它的方法(即您发布的方法)中将UIImageView的userInteractionEnabled属性设置为yes:

然后,您很可能需要将UIImageView子类化,并实现以下方法来移动图像视图:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

提示:在touchsmoved:etc.方法中,您需要跟踪用户手指的移动位置——在阅读更多文档后,您很可能会使用
-[UITouch locationInView:]
--在这种情况下,您需要将坐标转换为图像视图的superview,然后移动图像视图本身。

在创建UIImageView的方法(即您发布的方法)中,将UIImageView的userInteractionEnabled属性设置为yes:

然后,您很可能需要将UIImageView子类化,并实现以下方法来移动图像视图:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
提示:在touchsmoved:etc.方法中,您需要跟踪用户手指的移动位置——在阅读更多文档后,您很可能会使用
-[UITouch locationInView:][/code>——在这种情况下,您需要将坐标转换为图像视图的superview,然后移动图像视图本身