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



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


 实现人体各个部位点击

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




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值