Objective-c touch-events

I have a set of images and would like to know what I touched. How could I implement this ...? More precisely: "Home Class" will create several classes of images:

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

The image class looks something like this:

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

later, I use these touch-event-event methods also in the Image-class:

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

My problem at the moment: by touch, the Began / Ended methods will run regardless of where I touched the screen, but I would like to know which of the images was affected .....

+3
source share
2 answers

, , , . , , UIImage 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...
    }


}
+3

*.h(interface):

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

UIImageView *. xib '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".

+1

Source: https://habr.com/ru/post/1769956/


All Articles