可以触发点击事件并变色的UILabel

谁说UILabel不能够当做button处理点击事件呢?今天,笔者就像大家提供一个改造过的,能够触发点击事件并变色的UILabel:)

效果图:

还能当做计时器用囧:

源码如下:

TapLabel.h 与 TapLabel.m

//
// TapLabel.h
// TapLabel
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h>
@class TapLabel; @protocol TapLabelDelegate <NSObject>
- (void)tapLabelEvent:(TapLabel *)label;
@end @interface TapLabel : UILabel @property (nonatomic, assign) id<TapLabelDelegate> delegate; // 协议
@property (nonatomic, strong) NSString *notificationName; // 设置通知中心名字
@property (nonatomic, strong) NSDictionary *metaData; // 元数据 @end
//
// TapLabel.m
// TapLabel
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "TapLabel.h" @implementation TapLabel - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.userInteractionEnabled = YES;
UILongPressGestureRecognizer *tap = \
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(labelEvent:)];
tap.minimumPressDuration = 0.01f;
[self addGestureRecognizer:tap];
}
return self;
} - (void)labelEvent:(UILongPressGestureRecognizer *)gesture
{
// 获取到坐标值
CGPoint locationPoint = [gesture locationInView:self]; // 状态1
if (gesture.state == UIGestureRecognizerStateBegan)
{
self.highlighted = YES;
} // 状态2
if(gesture.state == UIGestureRecognizerStateChanged)
{
if (locationPoint.x <= self.bounds.size.width && locationPoint.x >= &&
locationPoint.y <= self.bounds.size.height && locationPoint.y >= )
{
self.highlighted = YES;
}
else
{
self.highlighted = NO;
}
} // 状态3
if (gesture.state == UIGestureRecognizerStateEnded)
{
if (locationPoint.x <= self.bounds.size.width && locationPoint.x >= &&
locationPoint.y <= self.bounds.size.height && locationPoint.y >= )
{ if (_delegate) {
[_delegate tapLabelEvent:self];
} if (_notificationName) {
[[NSNotificationCenter defaultCenter] postNotificationName:_notificationName
object:nil
userInfo:@{@"TapLabel": self}];
}
} self.highlighted = NO;
}
} @end

使用时的源码:

//
// RootViewController.m
// TapLabel
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "TapLabel.h" @interface RootViewController ()<TapLabelDelegate> @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; TapLabel *tap = [[TapLabel alloc] initWithFrame:CGRectMake(, , , )];
tap.textAlignment = NSTextAlignmentCenter;
tap.center = self.view.center;
tap.text = @"YouXianMing";
tap.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:];
tap.delegate = self;
tap.metaData = @{@"name": @"YouXianMing"}; tap.highlightedTextColor = [UIColor redColor];
[self.view addSubview:tap];
} - (void)tapLabelEvent:(TapLabel *)label
{
NSLog(@"%@", label.metaData);
} @end

原理解析:

1. 在初始化的时候后添加了手势处理:

2. 精确计算手势的3种状态

3. UILabel自带了highlightedTextColor:)

原理就是这么简单呢:)

 
 

可以触发点击事件并变色的UILabel的更多相关文章

  1. view.performClick()触发点击事件

    1.主要作用 自动触发控件的点击事件 2.界面的布局文件  activity_main.xml <RelativeLayout xmlns:android="http://schema ...

  2. 解决jQuery ajax动态新增节点无法触发点击事件的问题

    在写ajax加载数据的时候发现,后面添加进来的demo节点元素,失去了之前的点击事件.为什么点击事件失效,我们该怎么去解决呢? 其实最简单的方法就是直接在标签中写onclick="" ...

  3. 解决JavaScript拖动时同时触发点击事件的BUG

    在做在线地图项目的时候,在给marker点绑定事件时,因为有点击事件click,同时又存在拖动dragEnd事件,首先没有重大缺陷,就是在用户在点击的时候,有时候本想是点击,但是他触发了drag的事件 ...

  4. 关于AJAX异步加载节点无法触发点击事件问题的解决方式

    做练习的过程中遇到一个问题,使用AJAX异步新增一个节点,无法触发点击事件,经过查阅之后知道一个方式,使用JS的委托事件,在此做一个记录. $(document).on('click', '.recr ...

  5. JS实现按下按键触发点击事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. vue中Enter触发登录事件和javascript中Enter触发点击事件

    created(){ window.addEventListener('keydown', this.handleKeyDown, true)//开启监听键盘按下事件 } 在methods中当keyC ...

  7. Echarts如何添加鼠标点击事件?防止重复触发点击事件

    Echarts如何添加鼠标点击事件? 1.通常我们只使用了以下代码,通过配置项和数据显示图表. var myChart = echarts.init(document.getElementById(' ...

  8. heightcharts点击曲线图获取返回值的问题(ios点击图表第一次无法触发点击事件解决方法)

    需求:用的heightcharts插件,点击曲线图想获得所点击点的返回值,如图 问题代码: (function chart_line(){ var data={"title":[& ...

  9. js中 ajax动态新增节点无法触发点击事件

    在写ajax加载数据的时候发现,后面添加进来的demo节点元素,失去了之前的点击事件. 其实最简单的方法就是直接在标签中写onclick="",但是这样写有些场景的是实现不了的,最 ...

随机推荐

  1. CRF两个例子的理解

    概率计算例子: 预测例子:

  2. springboot-16-springboot中引入xml文件

    参考原文: http://412887952-qq-com.iteye.com/blog/2293846 使用的是在spring中注入一个bean的方式来测试是否成功, 感觉略不实用, 只碰到过一次d ...

  3. printf()的转换说明的修饰符中的标记、数字、和.数字

    先记下代码和运行结果 再解释 #include <stdio.h> #include <stdlib.h> #include <limits.h> #define ...

  4. Linux 文件IO管理 - POSIX

    以下是对POSIX的简短解释: POSIX表示可移植操作系统接口(Portable Operating System Interface of UNIX,缩写为 POSIX ),POSIX标准定义了操 ...

  5. 资料汇总--java开发程序员必备技能

    1.  熟练使用Java语言进行面向对象程序设计(面向对象:继承.多态.抽象): 有良好的编程习惯(阿里开发手册  链接:http://pan.baidu.com/s/1dFEA6cT 密码:kqj4 ...

  6. springboot之约定大约配置

    前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样 ...

  7. PHP 类与对象 全解析(三)

    目录 PHP 类与对象 全解析( 一) PHP 类与对象 全解析( 二) PHP 类与对象 全解析(三 ) 13.魔术方法 定义:PHP把所有以__(两个下划线)开头的类方法当成魔术方法     __ ...

  8. 【模板】 全排列 && 有重复元素的全排列

    全排列 #include<bits/stdc++.h> using namespace std; ]; void print (int n){ ;i<=n;i++) cout< ...

  9. 【转】SpringBoot系列之—瘦身部署

    一.前言 SpringBoot部署起来虽然简单,如果服务器部署在公司内网,速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼: 编译出来的 Jar 包很大,如果工程引入了许多开源组件 ...

  10. HTTP协议之内容协商

    一个URL常常需要代表若干不同的资源.例如那种需要以多种语言提供其内容的网站站点.如果某个站点有说法语的和说英语的两种用户,它可能想用这两种语言提供网站站点信息.理想情况下,服务器应当向英语用户发送英 ...