用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 ...
随机推荐
- XML CData 处理
调研了 JAXB.XMLMapper(jackson) 具体方式 实现 优势 JAXB 1. 需要增加 CDATA 的Adaptor 2. 需要增加对非CDATA 的 CharacterEscapeH ...
- linux tar 命令
tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件! ...
- Angular中的$q的形象解释及深入用法
作者:寸志链接:https://zhuanlan.zhihu.com/p/19622332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 早上,老爸说:“儿子,天气如何 ...
- Task.Factory.StartNew和Task.Run
在系统中单开线程进行操作,经常用到Task,发现Task主要有以下两种方法 Task.Factory.StartNew(() => { }); Task.Run(() => { }); 初 ...
- spark集群搭建(java)未完待续
环境 操作系统:windows10 虚拟机工具:VMware14.1 NUX版本:Centos7.2(64) JDK:1.8(64) 一.安装linux,master(桥接模式上网),slave(na ...
- [转]UI-Grid HeaderCellClass
本文转自:http://blog.csdn.net/vesong87/article/details/69230476 原文: 115 HeaderCellClass 在columnDef中可以为每个 ...
- c#基础学习(0629)之导出Excel方法
给予NPOI插件的方法,所以首先要下载NPOI插件:vs项目中点击“项目”==>“管理NoGet程序包”==>搜索“NPOI”然后下载==>using引入Controller代码: ...
- thinkphp 查询单个“年-月-日” FROM_UNIXTIME
*时间格式转换,使datetimepicker.js 能够搜索 查询 单个 年月日而不需是区间 查询. //另起一行看转换结果 select *,FROM_UNIXTIME(CreateTime,'% ...
- 532 -数组中的K-diff对
例1: 输入: [3,1,4,1,5],k = 2 输出: 2 说明:阵列中有两个2-diff对,(1,3)和(3,5). 虽然我们在输入中有两个1,但我们应该只返回唯一对的数量. 例2: 输入: ...
- npm saveError ENOENT: no such file or directory
1.报错情况 在执行npm install xxx时,出现如下:npm WARN saveError ENOENT: no such file or directory, open '/nodetes ...