从阵列和触摸屏查看UIImageView?对于IPhone

从阵列和触摸屏查看UIImageView?对于IPhone,iphone,uiimageview,Iphone,Uiimageview,我有这个方法 代码: 现在我用这个来调用图像 代码: 及 但触摸法只能在一张图像上工作,而不能同时在两张图像上工作 代码: 如何使此触摸同时适用于两者您正在addImageSubViewAtX方法中重新初始化相同的图像视图(myImage1),而不是创建和添加新的图像视图。因此,当您使用myImage1时,触摸法只能访问最新的图像视图 而是每次都添加一个新的图像视图。在添加之前给它一个特定的标签,并检查该标签在视图上是否有触动 比如: - (void)addImageSubViewAtX:(CG

我有这个方法 代码:

现在我用这个来调用图像 代码:

但触摸法只能在一张图像上工作,而不能同时在两张图像上工作

代码:


如何使此触摸同时适用于两者

您正在addImageSubViewAtX方法中重新初始化相同的图像视图(myImage1),而不是创建和添加新的图像视图。因此,当您使用myImage1时,触摸法只能访问最新的图像视图

而是每次都添加一个新的图像视图。在添加之前给它一个特定的标签,并检查该标签在视图上是否有触动

比如:

- (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y {
  CGRect myImageRect1 = CGRectMake(x, y, 30.0f, 30.0f); 
  UIImageView myImage1 = [[UIImageView alloc] initWithFrame:myImageRect1]; 
  [myImage1 setImage:[UIImage imageNamed:@"status_finish.gif"]];
  myImage1.tag = 1000;
  [self.view addSubview:myImage1];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
 UITouch *touch = [touches anyObject]; 
 if ([[touch view] tag] == 1000) 
 { 
     [pieMenu showInView:self.view atPoint:p]; 
 }
}
好,

您调用的是
-(void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y
-方法的2倍

在这个方法中,您将myImage1的指针设置为UIImageView的新实例。 因此,myImage1是对您添加的最后一个UIImageView的引用


这就是为什么它只适用于一个图像视图。

如果您将代码格式化为更具可读性,您将更有可能得到答案。看一下格式指南。你让我的一天过得真是太棒了:-)我挣扎了3个小时DAAMDon’别忘了在方法“addImageSubViewAtX…”的末尾发布myImage1
[self addImageSubViewAtX:160.0 atY:190.0];
[self addImageSubViewAtX:10.0 atY:190.0];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 
     CGPoint p = [touch locationInView:self.view]; 
     if (CGRectContainsPoint(CGRectMake(myImage1.frame.origin.x, myImage1.frame.origin.y, myImage1.frame.size.width, myImage1.frame.size.height ), p)) 
     { 
         [pieMenu showInView:self.view atPoint:p]; 
     }
}
- (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y {
  CGRect myImageRect1 = CGRectMake(x, y, 30.0f, 30.0f); 
  UIImageView myImage1 = [[UIImageView alloc] initWithFrame:myImageRect1]; 
  [myImage1 setImage:[UIImage imageNamed:@"status_finish.gif"]];
  myImage1.tag = 1000;
  [self.view addSubview:myImage1];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
 UITouch *touch = [touches anyObject]; 
 if ([[touch view] tag] == 1000) 
 { 
     [pieMenu showInView:self.view atPoint:p]; 
 }
}