通过path绘制点击区域

效果

源码

https://github.com/YouXianMing/Animations

//
// TapDrawImageView.h
// TapDrawImageView
//
// Created by YouXianMing on 16/5/9.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
#import "TapDrawPathManager.h"
@class TapDrawImageView; static NSString *tapDrawImageViewNormalState = @"normalState";
static NSString *tapDrawImageViewHighlightState = @"highlightState";
static NSString *tapDrawImageDisableState = @"disableState"; @protocol TapDrawImageViewDelegate <NSObject> - (void)tapDrawImageView:(TapDrawImageView *)tapImageView selectedPathManager:(TapDrawPathManager *)pathManager; @end @interface TapDrawImageView : UIView @property (nonatomic, weak) id <TapDrawImageViewDelegate> delegate; @property (nonatomic, strong) NSMutableArray <TapDrawPathManager *> *pathManagers; @end
//
// TapDrawImageView.m
// TapDrawImageView
//
// Created by YouXianMing on 16/5/9.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "TapDrawImageView.h" @interface TapDrawImageView () @property (nonatomic, strong) TapDrawPathManager *currentSelectedManager;
@property (nonatomic, strong) UIImageView *imageView; @end @implementation TapDrawImageView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.pathManagers = [NSMutableArray array];
self.backgroundColor = [UIColor clearColor];
} return self;
} - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); [self.pathManagers enumerateObjectsUsingBlock:^(TapDrawPathManager *pathManager, NSUInteger idx, BOOL *stop) { TapDrawObject *drawObject = [pathManager.colorsType objectForKey:pathManager.currentDrawType]; // Set Fill Color.
{
CGFloat red = ;
CGFloat green = ;
CGFloat blue = ;
CGFloat alpha = ;
[drawObject.fillColor getRed:&red green:&green blue:&blue alpha:&alpha];
CGContextSetRGBFillColor(context, red, green, blue, alpha);
} // Set Stroke Color.
{
CGFloat red = ;
CGFloat green = ;
CGFloat blue = ;
CGFloat alpha = ;
[drawObject.strokeColor getRed:&red green:&green blue:&blue alpha:&alpha];
CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
} pathManager.path.lineWidth = drawObject.lineWidth;
[pathManager.path fill];
[pathManager.path stroke];
}];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self]; [self.pathManagers enumerateObjectsUsingBlock:^(TapDrawPathManager *pathManager, NSUInteger idx, BOOL *stop) { if ([pathManager.path containsPoint:touchPoint]) { if (self.delegate && [self.delegate respondsToSelector:@selector(tapDrawImageView:selectedPathManager:)]) { [self.delegate tapDrawImageView:self selectedPathManager:pathManager];
} if ([pathManager.currentDrawType isEqualToString:tapDrawImageDisableState] == NO) { pathManager.currentDrawType = tapDrawImageViewHighlightState;
_currentSelectedManager = pathManager;
[self setNeedsDisplay];
} *stop = YES;
}
}];
} - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { if (_currentSelectedManager) { _currentSelectedManager.currentDrawType = tapDrawImageViewNormalState;
[self setNeedsDisplay];
}
_currentSelectedManager = nil;
} - (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { if (_currentSelectedManager) { _currentSelectedManager.currentDrawType = tapDrawImageViewNormalState;
[self setNeedsDisplay];
}
_currentSelectedManager = nil;
} @end

通过path绘制点击区域的更多相关文章

  1. 点击区域外隐藏该区域,event.stopPropagation()

    event.stopPropagation() Description: Prevents the event from bubbling up the DOM tree, preventing an ...

  2. Android上滑手势触发和不增加布局层级扩大点击区域

    最近项目中需要实现手势上滑或者点击滑出界面的效果,实现上是利用GestureDetector,然后在onFling中判断,但遇到一个问题:手势上滑是针对整个布局的,但如果有对单独的View设置点击监听 ...

  3. Android不规则点击区域详解

    Android不规则点击区域详解 摘要 今天要和大家分享的是Android不规则点击区域,准确说是在视觉上不规则的图像点击响应区域分发. 其实这个问题比较简单,对于很多人来说根本不值得做为一篇博文写出 ...

  4. HTML <map> 标签-创建带有可点击区域的图像映射

    定义和用法 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. 所有主流浏览器都支持 <map> 标签. 注释:area 元素永远嵌套在 map 元素内部. ...

  5. iOS 增加UIButton按钮的可点击区域

    在很多时候,按钮可能看起来那么大,但是在它周围进行点击时,都能够触发事件,是因为它的可点击区域比我们看到的button要大. 在使用AutoLayout的时候,我们处理的是按钮的image属性,所以这 ...

  6. 如何扩大ImageView的点击区域

    我们在开发中会遇到,给imageview设置点击事件,但是美工切的原始图片一般在24dp左右,这个尺寸点击时会出现不灵敏的情况(点击区域太小). 解决方案: 给imageView设置  scaleTy ...

  7. HTML <area> 标签 带有可点击区域的图像映射(图像映射指的是带有可点击区域的图像)

    例子: <img src="planets.gif" width="145" height="126" alt="Plane ...

  8. 带有可点击区域的图像映射:HTML <map> 标签

    实例 带有可点击区域的图像映射: <img src="planets.jpg" border="0" usemap="#planetmap&qu ...

  9. 第七篇、hitTest UITabbar中间突出按钮额外增加可点击区域

    简介: 以前UITabbar使用中间有一个凸起按钮时,常常就需要用到hitTest来处理可点击的范围. 示例代码: - (UIView *)hitTest:(CGPoint)point withEve ...

随机推荐

  1. InnoDB逻辑存储结构

    从InnoDB存储引擎的逻辑存储结构看,所有数据都被逻辑地存放在一个空间中,称之为表空间(tablespace).表空间又由段(segment).区(extent).页(page)组成.页在一些文档中 ...

  2. vscodes使用(一): 常用插件,在线与离线安装

    一.常用插件 1.Live server 浏览器实时刷新 插件安装成功后,会在底部工具栏中,显示个Go Live *.html文件,点击右键,可以看到live server两条指令   2.Esasy ...

  3. [转] React 最佳实践——那些 React 没告诉你但很重要的事

    前言:对很多 react 新手来说,网上能找到的资源大都是些简单的 tutorial ,它们能教会你如何使用 react ,但并不会告诉你怎么在实际项目中优雅的组织和编写 react 代码.用谷歌搜中 ...

  4. 开源项目TypeScript

    TypeScript 优秀开源项目大合集   TypeScript出来有段时间了,也冒出了很多用TypeScript开发的优秀开源项目,搜寻了一些基于TypeScript项目,分享给大家: https ...

  5. 008.MySQL-Keepalived搭配脚本02

    vim /etc/keepalived/check_MySQL.sh #!/bin/sh #isok=$(sed -n '2p' /etc/keepalived/result.txt) isok=$( ...

  6. 关于我学XSS躺过的那些坑

    XSS字符编码 在学习编码绕过时由于数量多,类型相似,不太容易记得住,记得全,故做此记录. 0x01 Html标签属性中执行 简单了解: Html标签属性中的XSS问题多属于javascript伪协议 ...

  7. 使用eclipse svn塔建(配置)时的一点点心得

    有没有人遇到下面这种情况??自己创建的SVN如下: 但网上别人搭建好的是这样子的: 就是为什么我的只有个主文件,而没有src.webroot.meta-inf.web-inf等子文件呢?? 这是我找了 ...

  8. beta6

    吴晓晖(组长) 过去两天完成了哪些任务 对手写输入进行了重构,然后重新捋了一下bayes的思路 展示GitHub当日代码/文档签入记录 接下来的计划 推荐算法 还剩下哪些任务 过去两天完成了哪些任务: ...

  9. Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 构造

    D. Dreamoon and Sets 题目连接: http://www.codeforces.com/contest/476/problem/D Description Dreamoon like ...

  10. CentOS安装CLI

    #使用root账号 vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epe ...