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

实现人体各个部位点击

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

IOS 多个ImageView图片层叠透明区域点击事件穿透的更多相关文章

  1. iOS 使点击事件穿透透明的UIView

    如图: 悬浮的三个按钮下方有一个可以点击的灰色区域,但是点击按钮之间的透明区域, 这三个按钮的contentView会响应这个点击事件,这时候需要让这个contentView不响应这个点击事件. 解决 ...

  2. Android EditText中插入图片并响应点击事件

    EditText中插入图片基本就是两种方法: ,通过Html.fromHtml(..)来实现 [mw_shl_code=java,true]eText.append(Html.fromHtml(&qu ...

  3. IOS开发中如何给UIImageView添加点击事件

    1.先创建一个UIImageView控件: photeImageView = [[UIImageView alloc]init]; photeImageView.frame = CGRectMake( ...

  4. iOS全埋点解决方案-控件点击事件

    前言 ​ 我们主要介绍如何实现控件点击事件($AppClick)的全埋点.在介绍如何实现之前,我们需要先了解一下,在 UIKit 框架下,处理点击或拖动事件的 Target-Action 设计模式. ...

  5. iOS UITapGestureRecognizer手势和UIButton 以及UITabelView点击事件冲突

    一:在同一个view上加载,UITapGestureRecognizer手势,UIButton 行为,UITabelView点击事件冲突: 二:解决方式: 在UITapGesttureRecogniz ...

  6. ios 给移动的控件添加点击事件

    前言: 给一个UIView做移动动画,虽然看起来frame在持续改变,但是它的frame已经是最终值了. 也就是说表面看到的动画都是假象,它的真实位置已经是固定的了.所以只有点击在他的真实frame范 ...

  7. ios执行失去焦点,不执行点击事件

    原因:由于JavaScript为单线程,同一时间只能执行处理一个事件.“blur优先于click执行”.而在本示例中,由于blur处理程序,会将对下拉框展示区隐藏,所以导致其后续click事件并不会执 ...

  8. Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件

    转载:http://blog.csdn.net/aminfo/article/details/7872681 经常会在项目中用到透明图片,不规则图片,特别是做游戏的时候,需要对图片的透明区域的点击事件 ...

  9. iOS开发之ImageView复用实现图片无限轮播

    在上篇博客中iOS开发之多图片无缝滚动组件封装与使用给出了图片无限轮播的实现方案之一,下面在给出另一种解决方案.今天博客中要说的就是在ScrollView上贴两个ImageView, 把ImageVi ...

随机推荐

  1. 关于js效果不提示就执行了刷新(解决 在h-ui框架中)

    parent.layer.msg('保存成功!<script>setTimeout("window.location.reload();",1100);<\/sc ...

  2. tar、zip 、unzip 打包与压缩 (参考:http://pengyl.blog.51cto.com/5591604/1191197)

    通常都是先通过tar命令将多个文件或目录打包成一个包文件,然后再通过gzip或bzip2进行压缩,如*.tar.gz和*.tar.bz2就属于这种先打包再压缩的文件.在实际使用中,一般都是通过tar命 ...

  3. mysql备份sql,脚本

    MySQL 安装位置:/usr/local/mysq 论坛数据库名称为:bbs MySQL root 密码:123456 数据库备份目的地:/var/db_backup/ #! /bin/bash / ...

  4. C# Winform 开发框架

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...

  5. 【转】深入 char * ,char ** ,char a[ ] ,char *a[] 内核

    原文出处:http://blog.csdn.net/daiyutage/article/details/8604720    C语言中由于指针的灵活性,导致指针能代替数组使用,或者混合使用,这些导致了 ...

  6. 很少有人知道的c++中的try块函数

    c++有一些在现实世界中很少看到的结构.这些结构有着自己的用法,但是要特别小心保守的予以运用.就像是网站 The Old New Thing首页标题上面的说的那样: “代码通常被读的次数原因超过了被写 ...

  7. 2016022607 - redis配置文件

    在Redis有配置文件(redis.conf)可在Redis的根目录下找到.可以通过Redis的CONFIG命令设置所有Redis的配置. Redis的CONFIG命令的基本语法如下所示: redis ...

  8. js对于工厂模式的理解

    有很多人对工厂模式有着这样或者那样不理解的地方,今天我就和大家分享一下我的心得. 其实工厂模式是基于面向对象的一种模式.大家先看这样的一段代码: 其实这个程序很简单,学过js的人都会写,基本没什么问题 ...

  9. 使用Markdown在博客里插入代码

    今天尝试了一下在线使用Markdown编辑器写博客,发现想要实现下面这样的效果还真得折腾一会儿. <html> <head> <meta charset="ut ...

  10. jquery deferred

    http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html http:// ...