从阵列和触摸UIImageView?对于iPhone

时间:2010-07-14 08:23:24

标签: iphone uiimageview

我有这种方法 代码:

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

}

现在我用它来调用图像 代码:

[self addImageSubViewAtX:160.0 atY:190.0];

[self addImageSubViewAtX:10.0 atY:190.0];

但触摸方法仅适用于1张图片而不是两张图片

代码:

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

如何使这种触摸适用于他们两个

2 个答案:

答案 0 :(得分:0)

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

每次都添加一个新的图像视图。在添加之前为其指定特定标记,并使用该标记检查视图上的触摸。

类似的东西:

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

答案 1 :(得分:0)

好的,

你正在调用- (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y - 方法的两倍。

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

这就是为什么它只适用于一个imageView。