2010-10-17 33 views
3

我有一組圖像,並想知道我曾經觸摸過哪些圖像。我怎麼能實現...? 爲了更準確: 「家級」將實例一對夫婦形象 - 類:objective-c觸摸事件

Image *myImageView = [[Image alloc] initWithImage:myImage]; 

圖像類看起來是這樣的:

- (id) initWithImage: (UIImage *) anImage 
{ 
    if ((self = [super initWithImage:anImage])) 
    { 
     self.userInteractionEnabled = YES; 
    } 
    return self; 
} 

後來,我用這些觸摸事件的方法也是在圖像類:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{} 
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{} 

我此刻的問題:的touchesBegan /已結束的方法將在那裏我被感動無論解僱屏幕上,但我想知道哪些圖像已被觸摸.....

回答

3

無論何時您碰到觸摸,您都會檢查觸摸是否發生在圖像區域之間。這裏是示例代碼,假設你有一個名爲img的UIImage對象。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 

    CGPoint location = [touch locationInView:self.view]; 

    if (location.x >= img.x && location.x <= img.x && location.y >= img.y && location.y <= img.y) { 
     // your code here... 
    } 


} 
+1

我知道這是舊的,但不應該有條件的,'if(location.x> = img.frame.origin.x && location.x <= img.frame.origin.x + img.frame。 size.width && location.y> = img.frame.origin.y && location.y <= img.frame.origin.y + img.frame.size.height)'? – 2014-01-22 22:42:24

1

裏面你的* .h(接口)文件:在您*的.xib

@interface MyViewController : UIViewController{ 
IBOutlet UIImageView *imageViewOne; 
IBOutlet UIImageView *imageViewTwo; 
UIImageView * alphaImage; 
} 
-(BOOL)isTouch:(UITouch *)touch WithinBoundsOf:(UIImageView *)imageView; 

廣場的UIImageView組件,並使用與'imageViewOne'它們綁定 'imageViewTwo'「文件所有者」

轉到* .M(實現)文件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    if ([self isTouch:touch WithinBoundsOf:imageViewOne]) 
    { 
     NSLog(@"Fires first action..."); 
    } 
    else if([self isTouch:touch WithinBoundsOf:imageViewTwo]){ 
     NSLog(@"Fires second action..."); 
    } 
} 

//(Optional 01) This is used to reset the transparency of the touched UIImageView 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [alphaImage setAlpha:1.0]; 
} 

-(BOOL)isTouch:(UITouch *)touch WithinBoundsOf:(UIImageView *)imageView{ 

    CGRect _frameRectangle=[imageView frame]; 
    CGFloat _imageTop=_frameRectangle.origin.y; 
    CGFloat _imageLeft=_frameRectangle.origin.x; 
    CGFloat _imageRight=_frameRectangle.size.width+_imageLeft; 
    CGFloat _imageBottom=_frameRectangle.size.height+_imageTop; 

    CGPoint _touchPoint = [touch locationInView:self.view]; 

    /*NSLog(@"image top %f",_imageTop); 
    NSLog(@"image bottom %f",_imageBottom); 
    NSLog(@"image left %f",_imageLeft); 
    NSLog(@"image right %f",_imageRight); 
    NSLog(@"touch happens at %f-%f",_touchPoint.x,_touchPoint.y);*/ 

    if(_touchPoint.x>=_imageLeft && 
     _touchPoint.x<=_imageRight && 
     _touchPoint.y>=_imageTop && 
     _touchPoint.y<=_imageBottom){ 

     [imageView setAlpha:0.5];//optional 01 -adds a transparency changing effect 
     alphaImage=imageView;//optional 01 -marks the UIImageView which deals with the transparency changing effect for the moment. 

     return YES; 
    }else{ 
     return NO; 
    } 
} 

這就是我如何處理這一點。我讀到了「itsaboutcode」的帖子。