【问题标题】:how to make general collision with images in for loop如何在for循环中与图像进行一般碰撞
【发布时间】:2010-07-14 03:45:15
【问题描述】:

collison 但它仅适用于 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]; 
     }
} 

这将适用于

[self addImageSubViewAtX:160.0 atY:190.0];

或

[self addImageSubViewAtX:90.0 atY:140.0];

但不在一起

这是你定义的方法

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

【问题讨论】:

  • 我认为如果两个图像都需要这个,对吧?所以,再放一个 if 可以解决 if (CGRectContainsPoint(CGRectMake(myImage1.frame.origin.x, myImage1.frame.origin.y, myImage1 .frame.size.width, myImage1.frame.size.height ), p))
  • 我做了但没有工作:(任何想法

标签: iphone uiimageview collision-detection


【解决方案1】:

我建议让一个方法接收一些参数,因为它们并不完全相同

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

}

然后你调用它 2 次:[self addImageSubViewAtX:160.0 atY:190.0] and [self addImageSubViewAtX:90.0 atY:140.0]

【讨论】:

  • 谢谢,但我应该在哪里调用 [self addImageSubViewAtX:160.0 atY:190.0]; [self addImageSubViewAtX:90.0 atY:140.0];我希望他们在 viewdidload()
  • 是的,你可以在 viewDidLoad() 中调用它们
  • 完美现在一个问题是我正在为 collison 执行此操作,但它仅适用于 1 个图像 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [触摸任何对象]; CGPoint p = [触摸 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在点:p]; } }
  • 您能否编辑问题以添加它。这是一个很长的代码,我很难看到。我这里没有 XCode
猜你喜欢
  • 2013-08-29
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多