2010-07-14 66 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