2010-10-17 7 views
3

J'ai un ensemble d'images et j'aimerais savoir ce que j'ai touché. Comment pourrais-je implémenter cela ...? Pour être plus précis: A "Home-Class" instanciera quelques images Classes:objectif-c touch-events

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

La classe d'image ressemble à quelque chose comme ceci:

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

plus tard, je l'utilise ces petites touches-événement-méthodes aussi dans l'image classe:

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

Mon problème au moment: les méthodes touchesBegan/Ended seront tirés, peu importe où je touchais l'écran, mais je voudrais savoir lequel des images a été touché .....

Répondre

3

Chaque fois que vous obtenez le toucher, vous vérifiez si ce contact se produit entre votre zone d'image. Voici l'exemple de code, supposons que vous ayez un objet UIImage appelé img.

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

Je sais que c'est vieux, mais ne devrait pas être le conditionnel, '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) '? –

1

l'intérieur de votre * .h (interface) fichier:

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

Placez les composants de UIImageView sur votre * .xib et les lier avec 'imageViewOne' et 'imageViewTwo' utilisant le "Propriétaire du fichier".

Aller à la * .m (mise en œuvre) et fichier:

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

Voilà comment je me suis occupé que. J'ai eu l'idée d'avoir lu le post de "itsaboutcode".