Objective-C触摸事件

3

我有一组图片,想知道哪些被修改过。如何实现这个功能呢?更准确地说,一个“主页类”将实例化几个“图像类”:

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

图像类大致如下所示:
- (id) initWithImage: (UIImage *) anImage 
{
    if ((self = [super initWithImage:anImage])) 
    {
        self.userInteractionEnabled = YES;
    }
    return self;
}

稍后,我也在Image类中使用这些触摸事件方法:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

我目前遇到的问题是:无论我在屏幕的哪个位置触摸,都会触发touchesBegan/Ended方法,但我想找出哪个图像被触摸了.....

2个回答

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) - Michael Hogenson

1

在你的*.h(接口)文件中:

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

在你的 *.xib 上放置 UIImageView 组件,并使用 "File's owner" 将它们绑定到 '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”的帖子中得到了灵感。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接