2010-07-14 67 views
1

科里森,但其只工作了1个图像如何在for循环

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

OR

[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];   
} 
+0

我认为你需要这个如果对于这两个图像,对吗?所以,把另一个如果可能解决 if(CGRectContainsPoint(CGRectMake(myImage1.frame.origin.x,myImage1.frame.origin.y,myImage1.frame.size.width,myImage1.frame.size .height),p)) – vodkhang 2010-07-14 04:45:04

+0

我做过但没有工作:(任何想法 – ram 2010-07-14 06:09:06

回答

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

} 

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

+0

谢谢,但我应该在哪里打电话 \t [self addImageSubViewAtX:160.0 atY:190.0]; \t [self addImageSubViewAtX:90.0 atY:140.0]; 我希望他们在viewDidLoad中() – ram 2010-07-14 04:00:12

+0

是的,你可以叫他们viewDidLoad中() – vodkhang 2010-07-14 04:10:27

+0

完美,现在的一个问题是有 我这样做了科里森,但其只工作了1个图像 - (空)的touchesBegan:(NSSet中*)触及事件:(UIEvent *)event { \t UITouch * touch = [touches anyObject]; \t CGPoint p = [touch locationInView:self.view]; \t \t \t \t \t \t \t \t \t 如果(CGRectContainsPoint(CGRectMake(myImage1.frame.origin.x,myImage1.frame.origin.y,myImage1.frame.size.width,myImage1。 frame.size.height),p)){ \t \t \t \t [pieMenu showInView:self.view atPoint:p]; \t} \t } – ram 2010-07-14 04:14:58