用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的更多相关文章

  1. 封装Button ,封装UITableView,封装UICollectionView

    ---恢复内容开始--- 封装Button ,封装UITableView,封装UICollectionView: 1.实现Button的创建和点击事件不用分开操作处理; 2.实现UITableView ...

  2. 控件类——Button、UIControlState状态、title及其属性

    封装: 封装按钮:1.有提示文字 —>UILable 2.并且可以点击 —> UIControl UIButton:是一个按钮(系统已经把UIControl封装好了).  里面可以放文字. ...

  3. IOS开发之自定义Button(集成三种回调模式)

    前面在做东西的时候都用到了storyboard,在今天的代码中就纯手写代码自己用封装个Button.这个Button继承于UIView类,在封装的时候用上啦OC中的三种回调模式:目标动作回调,委托回调 ...

  4. iOS-button利用block封装按钮事件【runtime 关联】

    用block封装最常用的就是网络请求的回调,其实也可以结合category封装button的按钮事件,同时利用runtime的对象关联: UIButton+wkjButton.h 文件 #import ...

  5. 自定义子tabBar

    基本设置 设置APPIcon(直接拖图片) 设置启动图片 将launch Screen File里的LaunchScreen.xib给删掉 点击launch image source框内的Use As ...

  6. Runtime的相关知识

    Runtime是近年来面试遇到的一个高频方向,也是我们平时开发中或多或少接触的一个领域,那么什么是runtime呢?它又可以用来做什么呢? 什么是Runtime?平时项目中有用过么? OC是一门动态性 ...

  7. 分享iOS开发常用(三方类库,工具,高仿APP,实用网站,技术干货)

    一 . JSONModel  (三方类库会有更新,建议大家在线下载) http://pan.baidu.com/s/1i5ybP1z 二.AFNetworkiong http://pan.baidu. ...

  8. android 滚动视图(ScrollView)

    为了可以让内嵌布局管理器之中加入多个显示的组件,而且又保证程序不这么冗余,所以可以通过 Activity程序进行控制,向内嵌布局管理器中添加多个组件. ScrollView提供一个显示的容器,可以包含 ...

  9. tabBar自定义

    有时系统的tabBar并不能满足我们的开发需求: 这时,我们需要自定义一个tabBar.直接上代码: // 在tabBarController中用KVC更换掉系统tabBar [self setVal ...

随机推荐

  1. JAVA 对象的行为 总结

    对象的行为 1. 断点调试 a:定位(设置断点)  b:启动调试  c:单步执行  观察变量(F5单步执行   F6单步跳过)d:修改2 static  静态   静态成员,为类的所有对象共享   在 ...

  2. java ee 中 Jsp 页面的定时的跳转(数字倒数)

    java ee 中 Jsp 页面的定时的跳转,实现数字倒计时跳转固定页面 1,Servlet类  RefreshServlet类实现 package org.servlet; import java. ...

  3. vuex中怎么把‘库’中的状态对象赋值给内部对象(三种方法)

    一.通过computed的计算属性直接赋值 import store from '@/store/store' export default{ name: 'count', data(){ retur ...

  4. 破解栅栏密码python脚本

    今天遇到一个要破解的栅栏密码,写了个通用的脚本 #!/usr/bin/env python # -*- coding: gbk -*- # -*- coding: utf_8 -*- # Author ...

  5. sql中全文检索CHARINDEX 和PATINDEX 区别

    最近经常使用字符串查找功能. 包括 1.全匹配查找字符串 2.模糊查找字符串 CHARINDEX 和 PATINDEX 函数都返回指定模式的开始位置.PATINDEX 可使用通配符,而 CHARIND ...

  6. 淺談 Entity 的概念

    延續上一篇文章<Drupal Commerce 概念架構>,本來打算要繼續講 Commerce 與 Views 整合的主題.不過由於這個主題牽涉到 Views 中的 Relationshi ...

  7. Sqlserver 备份

    Transact-SQL   语法规则 Transact-SQL   引用中的语法关系图使用下列规则. 大写    : Transact-SQL   关键字.   斜体    : Transact-S ...

  8. template-web.js 真分页绑定表格

     <div class="layui-table-box">                        <div class="layui-tabl ...

  9. Android Studio开发笔记

    工欲善其事,必先利其器. 来分享下一些tips吧. android studio优化 我的习惯是从visual studio沿袭过来的,所以快捷键都是参照VS改过来的. 设置调优 不打开上次打开的工程 ...

  10. Spark你需要知道这些

    谈到 Spark,我们总是强调它比 Hadoop 更高效.为什么它可以更高效呢?是因为它优先使用内存存储?还是因为它拥有比 MapReduce 更简单高效的计算模型? 与 Hadoop 作业的区别 我 ...