IOS 多个ImageView图片层叠透明区域点击事件穿透

经常用到多个透明图片层叠,但又需要获取不同图片的点击事件,本文实现图片透明区域穿透点击事件

 

 实现人体各个部位点击

  1. - (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event  
  2. {  
  3.     CGPoint shoulderPoint = [self getNewPoint:point SetImage:shouldImage];  
  4.     if(CGRectContainsPoint(shouldImage.bounds,shoulderPoint)) {  
  5.         if ([self isAplphaSetPoint:shoulderPoint andSetImage:shouldImage]) {  
  6.             shouldImage.image = [UIImage imageNamed:@"man_shoulder_pressed"];  
  7.             return YES;  
  8.         }  
  9.     }  
  10.     return YES;  
  11. }  
  12.   
  13. #param  point点转换  
  14. -(CGPoint) getNewPoint:(CGPoint) point SetImage:(UIImageView *) iv {  
  15.     return  CGPointMake(point.x - iv.frame.origin.x,  
  16.                         point.y - iv.frame.origin.y);  
  17. }  
  18.   
  19. -(BOOL) isAplphaSetPoint:(CGPoint) point andSetImage:(UIImageView *) iv {  
  20.     NSLog(@"point: %f", point.y);  
  21.     UIColor *uColor = [self colorAtPixel: point setImage: iv];  
  22.     const CGFloat *components = CGColorGetComponents(uColor.CGColor);  
  23.     if (NULL != components) {  
  24.         NSLog(@"Red: %f Green: %f Blue: %f alpha: %f", components[0], components[1], components[2], components[3]);  
  25.         float aplphaF = components[3];  
  26.         if ((aplphaF >= 0.5)) {  
  27.             return YES;  
  28.         }  
  29.     }  
  30.     return NO;  
  31. }  
  32. #param 点击时间结束 逻辑处理  
  33. -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {  
  34.   
  35. }  
  36.   
  37. - (UIColor *)colorAtPixel:(CGPoint)point setImage: (UIImageView *) iv {  
  38.     if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, iv.frame.size.width, iv.frame.size.height), point)) {  
  39.         return nil;  
  40.     }  
  41.       
  42.     NSInteger pointX = trunc(point.x);  
  43.     NSInteger pointY = trunc(point.y);  
  44.     CGImageRef cgImage = iv.image.CGImage;  
  45.     NSUInteger width = iv.frame.size.width;  
  46.     NSUInteger height = iv.frame.size.height;  
  47.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
  48.     int bytesPerPixel = 4;  
  49.     int bytesPerRow = bytesPerPixel * 1;  
  50.     NSUInteger bitsPerComponent = 8;  
  51.     unsigned char pixelData[4] = { 0, 0, 0, 0 };  
  52.     CGContextRef context = CGBitmapContextCreate(pixelData,  
  53.                                                  1,  
  54.                                                  1,  
  55.                                                  bitsPerComponent,  
  56.                                                  bytesPerRow,  
  57.                                                  colorSpace,  
  58.                                                  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);  
  59.     CGColorSpaceRelease(colorSpace);  
  60.     CGContextSetBlendMode(context, kCGBlendModeCopy);  
  61.       
  62.     // Draw the pixel we are interested in onto the bitmap context  
  63.     CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);  
  64.     CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);  
  65.     CGContextRelease(context);  
  66.       
  67.     // Convert color values [0..255] to floats [0.0..1.0]  
  68.     CGFloat red   = (CGFloat)pixelData[0] / 255.0f;  
  69.     CGFloat green = (CGFloat)pixelData[1] / 255.0f;  
  70.     CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;  
  71.     CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;  
  72.     return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];  
 
posted @ 2016-09-19 11:44  木木文  阅读(1093)  评论(0编辑  收藏  举报