繁体   English   中英

UIImageView从数组和触摸? 适用于iPhone

[英]UIImageView from array and touch? for IPhone

我有此方法代码:

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

如何使这种感觉对他们两个都起作用

您将在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]; 
 }
}

好的,

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

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

这就是为什么它仅适用于一个imageView的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM