用UIControl封装Button
用UIControl封装Button

效果

说明
UIControl在处理超出触摸范围的触摸事件时有bug
源码
基础类
//
// BaseControl.h
// BaseControl
//
// Created by YouXianMing on 15/8/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface BaseControl : UIControl /**
* ==== 由子类重写 ====
*
* 开始触发事件
*/
- (void)beginTouch; /**
* ==== 由子类重写 ====
*
* 结束触发事件
*
* @param outRange 是否超出操作范围
*/
- (void)endTouchOutOfRange:(BOOL)outRange; @end
//
// BaseControl.m
// BaseControl
//
// Created by YouXianMing on 15/8/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "BaseControl.h" @interface BaseControl () @property (nonatomic) CGPoint endPoint; @end @implementation BaseControl #pragma mark -
- (void)beginTouch { } - (void)endTouchOutOfRange:(BOOL)outRange { } #pragma mark - UIControl事件
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [self beginTouch]; return YES;
} - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { return YES;
} - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { self.endPoint = [touch locationInView:self];
} - (void)cancelTrackingWithEvent:(UIEvent *)event { [super cancelTrackingWithEvent:event];
} - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { CGPoint point = self.endPoint;
if (point.x >= && point.x <= self.bounds.size.width && point.y >= && point.y <= self.bounds.size.height) { [self endTouchOutOfRange:NO];
[super sendAction:action to:target forEvent:event]; } else { [self endTouchOutOfRange:YES];
[super sendAction:action to:target forEvent:event];
}
} @end
继承子类
//
// ColorButton.h
// BaseControl
//
// Created by YouXianMing on 15/8/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "BaseControl.h" @interface ColorButton : BaseControl @end
//
// ColorButton.m
// BaseControl
//
// Created by YouXianMing on 15/8/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ColorButton.h" @implementation ColorButton - (void)beginTouch { [UIView animateWithDuration:0.4f delay:.f usingSpringWithDamping:.f initialSpringVelocity:.f options: animations:^{ self.backgroundColor = [UIColor redColor]; } completion:^(BOOL finished) { }];
} - (void)endTouchOutOfRange:(BOOL)outRange { [UIView animateWithDuration:0.4f delay:.f usingSpringWithDamping:.f initialSpringVelocity:.f options: animations:^{ self.backgroundColor = [UIColor whiteColor]; } completion:^(BOOL finished) { }];
} @end
使用
//
// ViewController.m
// BaseControl
//
// Created by YouXianMing on 15/8/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "ColorButton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ColorButton *control = [[ColorButton alloc] initWithFrame:CGRectMake(, , , )];
control.center = self.view.center;
control.layer.borderWidth = .f;
[self.view addSubview:control]; [control addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
} - (void)buttonEvent:(id)sender { } @end
细节

用UIControl封装Button的更多相关文章
- 封装Button ,封装UITableView,封装UICollectionView
---恢复内容开始--- 封装Button ,封装UITableView,封装UICollectionView: 1.实现Button的创建和点击事件不用分开操作处理; 2.实现UITableView ...
- 控件类——Button、UIControlState状态、title及其属性
封装: 封装按钮:1.有提示文字 —>UILable 2.并且可以点击 —> UIControl UIButton:是一个按钮(系统已经把UIControl封装好了). 里面可以放文字. ...
- IOS开发之自定义Button(集成三种回调模式)
前面在做东西的时候都用到了storyboard,在今天的代码中就纯手写代码自己用封装个Button.这个Button继承于UIView类,在封装的时候用上啦OC中的三种回调模式:目标动作回调,委托回调 ...
- iOS-button利用block封装按钮事件【runtime 关联】
用block封装最常用的就是网络请求的回调,其实也可以结合category封装button的按钮事件,同时利用runtime的对象关联: UIButton+wkjButton.h 文件 #import ...
- 自定义子tabBar
基本设置 设置APPIcon(直接拖图片) 设置启动图片 将launch Screen File里的LaunchScreen.xib给删掉 点击launch image source框内的Use As ...
- Runtime的相关知识
Runtime是近年来面试遇到的一个高频方向,也是我们平时开发中或多或少接触的一个领域,那么什么是runtime呢?它又可以用来做什么呢? 什么是Runtime?平时项目中有用过么? OC是一门动态性 ...
- 分享iOS开发常用(三方类库,工具,高仿APP,实用网站,技术干货)
一 . JSONModel (三方类库会有更新,建议大家在线下载) http://pan.baidu.com/s/1i5ybP1z 二.AFNetworkiong http://pan.baidu. ...
- android 滚动视图(ScrollView)
为了可以让内嵌布局管理器之中加入多个显示的组件,而且又保证程序不这么冗余,所以可以通过 Activity程序进行控制,向内嵌布局管理器中添加多个组件. ScrollView提供一个显示的容器,可以包含 ...
- tabBar自定义
有时系统的tabBar并不能满足我们的开发需求: 这时,我们需要自定义一个tabBar.直接上代码: // 在tabBarController中用KVC更换掉系统tabBar [self setVal ...
随机推荐
- GBDT分类和回归例子
- 软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比
本文出自 “抚琴煮酒” 博客,出处http://andrewyu.blog.51cto.com/1604432/697466 现在网站发展的趋势对网络负载均衡的使用是随着网站规模的提升根据不同的阶 ...
- Maven 添加 Tomcat 插件
若想在 Maven 中使用 Tomcat 服务器,需要在 pom.xml 文件中的 <build></build> 标签中添加以下代码 <!-- Maven项目编译插件 ...
- [九省联考 2018]IIIDX
Description 题库链接 给你 \(n+1\) 个节点的一棵树,节点编号为 \(0\sim n\) , \(0\) 为根.边集为 \(\mathbb{E}=\left\{(u,v)\big|\ ...
- Java8新特性--流(Stream)
1.简介 Java 8是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和JVM等方面的十多个新特性.在本文中我们一起来学习引入的一个新特性- ...
- mybatis在oracle中的分页扩展
applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...
- Ubuntu16.04 LTS上安装Go1.10
原因 Ubuntu资源库上默认使用的是Go1.6.2版本,给最新版本代码编译带来了不少问题.本文就记录下在Ubuntu下直接安装Go最新版1.10的步骤. 准备工作 1.卸载已有版本 # 卸载已经安装 ...
- Centos7初次开机提示Initial setup of CentOS Linux 7 (core)
安装完成centos7后出现如下提示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License informat ...
- css3中Animation
CSS3我在5年之前就有用了,包括公司项目都一直在很前沿的技术. 最近在写慕课网的七夕主题,用了大量的CSS3动画,但是真的沉淀下来仔细的去深入CSS3动画的各个属性发现还是很深的,这里就写下关于帧动 ...
- mysql 删除某一个数据库下面的所有表,但不删除数据库
删除某一个数据库下面的所有表,但不删除数据库.该语句经过从concat拼接,最后查询出来的是删除表的语句,然后执行那些查询出来的语句就ok了select concat(‘drop table ‘,ta ...