经常用到多个透明图片层叠,但又需要获取不同图片的点击事件,本文实现图片透明区域穿透点击事件
实现人体各个部位点击
- - (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
- {
- CGPoint shoulderPoint = [self getNewPoint:point SetImage:shouldImage];
- if(CGRectContainsPoint(shouldImage.bounds,shoulderPoint)) {
- if ([self isAplphaSetPoint:shoulderPoint andSetImage:shouldImage]) {
- shouldImage.image = [UIImage imageNamed:@"man_shoulder_pressed"];
- return YES;
- }
- }
- return YES;
- }
- #param point点转换
- -(CGPoint) getNewPoint:(CGPoint) point SetImage:(UIImageView *) iv {
- return CGPointMake(point.x - iv.frame.origin.x,
- point.y - iv.frame.origin.y);
- }
- -(BOOL) isAplphaSetPoint:(CGPoint) point andSetImage:(UIImageView *) iv {
- NSLog(@"point: %f", point.y);
- UIColor *uColor = [self colorAtPixel: point setImage: iv];
- const CGFloat *components = CGColorGetComponents(uColor.CGColor);
- if (NULL != components) {
- NSLog(@"Red: %f Green: %f Blue: %f alpha: %f", components[0], components[1], components[2], components[3]);
- float aplphaF = components[3];
- if ((aplphaF >= 0.5)) {
- return YES;
- }
- }
- return NO;
- }
- #param 点击时间结束 逻辑处理
- -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- }
- - (UIColor *)colorAtPixel:(CGPoint)point setImage: (UIImageView *) iv {
- if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, iv.frame.size.width, iv.frame.size.height), point)) {
- return nil;
- }
- NSInteger pointX = trunc(point.x);
- NSInteger pointY = trunc(point.y);
- CGImageRef cgImage = iv.image.CGImage;
- NSUInteger width = iv.frame.size.width;
- NSUInteger height = iv.frame.size.height;
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- int bytesPerPixel = 4;
- int bytesPerRow = bytesPerPixel * 1;
- NSUInteger bitsPerComponent = 8;
- unsigned char pixelData[4] = { 0, 0, 0, 0 };
- CGContextRef context = CGBitmapContextCreate(pixelData,
- 1,
- 1,
- bitsPerComponent,
- bytesPerRow,
- colorSpace,
- kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
- CGColorSpaceRelease(colorSpace);
- CGContextSetBlendMode(context, kCGBlendModeCopy);
- // Draw the pixel we are interested in onto the bitmap context
- CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
- CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
- CGContextRelease(context);
- // Convert color values [0..255] to floats [0.0..1.0]
- CGFloat red = (CGFloat)pixelData[0] / 255.0f;
- CGFloat green = (CGFloat)pixelData[1] / 255.0f;
- CGFloat blue = (CGFloat)pixelData[2] / 255.0f;
- CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
- return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
- }