通过path绘制点击区域
通过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绘制点击区域的更多相关文章
- 点击区域外隐藏该区域,event.stopPropagation()
event.stopPropagation() Description: Prevents the event from bubbling up the DOM tree, preventing an ...
- Android上滑手势触发和不增加布局层级扩大点击区域
最近项目中需要实现手势上滑或者点击滑出界面的效果,实现上是利用GestureDetector,然后在onFling中判断,但遇到一个问题:手势上滑是针对整个布局的,但如果有对单独的View设置点击监听 ...
- Android不规则点击区域详解
Android不规则点击区域详解 摘要 今天要和大家分享的是Android不规则点击区域,准确说是在视觉上不规则的图像点击响应区域分发. 其实这个问题比较简单,对于很多人来说根本不值得做为一篇博文写出 ...
- HTML <map> 标签-创建带有可点击区域的图像映射
定义和用法 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. 所有主流浏览器都支持 <map> 标签. 注释:area 元素永远嵌套在 map 元素内部. ...
- iOS 增加UIButton按钮的可点击区域
在很多时候,按钮可能看起来那么大,但是在它周围进行点击时,都能够触发事件,是因为它的可点击区域比我们看到的button要大. 在使用AutoLayout的时候,我们处理的是按钮的image属性,所以这 ...
- 如何扩大ImageView的点击区域
我们在开发中会遇到,给imageview设置点击事件,但是美工切的原始图片一般在24dp左右,这个尺寸点击时会出现不灵敏的情况(点击区域太小). 解决方案: 给imageView设置 scaleTy ...
- HTML <area> 标签 带有可点击区域的图像映射(图像映射指的是带有可点击区域的图像)
例子: <img src="planets.gif" width="145" height="126" alt="Plane ...
- 带有可点击区域的图像映射:HTML <map> 标签
实例 带有可点击区域的图像映射: <img src="planets.jpg" border="0" usemap="#planetmap&qu ...
- 第七篇、hitTest UITabbar中间突出按钮额外增加可点击区域
简介: 以前UITabbar使用中间有一个凸起按钮时,常常就需要用到hitTest来处理可点击的范围. 示例代码: - (UIView *)hitTest:(CGPoint)point withEve ...
随机推荐
- 【pytorch】pytorch学习笔记(一)
原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...
- 3种高效的Tags标签系统数据库设计方案分享
需求背景 目前主流的博客系统.CMS都会有一个TAG标签系统,不仅可以让内容链接的结构化增强,而且可以让文章根据Tag来区分.相比传统老式的Keyword模式,这种Tag模式可以单独的设计一个Map的 ...
- read命令
read命令从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合. 该命令可以一次读取多个变量的值,变量和输入的值都需要使用空格隔开.在read命令后面,如果没有指定变量名,读取的数据将被 ...
- 【AtCoder】AGC020
A - Move and Win 题解 看两个人相遇的时候谁先手即可,相遇之后第一个移动的人必输 代码 #include <bits/stdc++.h> #define fi first ...
- Ubuntu服务器上相关软件或应用时常打不开的问题
于接触linux系统时间不就,所以在操作上难免会出现失误,以下两个问题就是近期经常出现的问题,具体如下: 1.ubuntu服务器上的浏览器时常打不开. 2.安装的pycharm和系统自带的pychar ...
- BeautifulSoup使用总结
一.介绍 BeautifulSoup为一个python库,它可以接收一个HTML或XML的字符串或文件,并返回一个BeautifulSoup对象,之后我们可以使用BeautifulSoup提供的众多方 ...
- html (第四本书第1~3章参考)
前三章都不会的话 呵呵……
- Android-多进程初识
Android-多进程初识 学习自 <Android开发艺术探索> https://baike.baidu.com/item/%E8%BF%9B%E7%A8%8B/382503?fr=al ...
- 百度 Echart 的使用
百度 Echarts 的使用 一.Echarts 简介 官方网站:http://echarts.baidu.com/ 下载地址:http://echarts.baidu.com/download.ht ...
- Codeforces Round #505 (Div 1 + Div 2) (A~D)
目录 Codeforces 1025 A.Doggo Recoloring B.Weakened Common Divisor C.Plasticine zebra D.Recovering BST( ...