用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

细节

最新文章

  1. iOS开发之地图与定位
  2. FunsionCharts图标控件点击
  3. PostSharp-4.3.22安装包_KeyGen发布
  4. Pull解析器学习
  5. android 项目学习随笔十八(三级缓存)
  6. JS 用window.open()函数,父级页面如何取到子级页面的返回值?
  7. error C2065:未声明的标识符错误
  8. 从汇编看c++中成员函数指针(一)
  9. SAP HANA中创建层次分析视图(Hierarchy Analytic View)
  10. Ubuntu14.04部署pyspider的过程
  11. 使用PIP扩展BTARN
  12. jdbc url写法(集群)
  13. 使用 GCD 实现倒计时效果
  14. jexus docker
  15. Node.js学习笔记(1)--一个最简单的服务器请求
  16. python string 模块
  17. ios 9 http
  18. CMD命令行窗口 复制黏贴
  19. 29、java中阻塞队列
  20. as3 阻止后续侦听器

热门文章

  1. #!/usr/bin/python和#!/usr/bin/env 的区别
  2. [转] Ubuntu 14.04/14.10下安装VMware Workstation 11图文教程
  3. Leetcode 647. Palindromic Substrings
  4. Java序列化机制和原理
  5. 功能------常用快捷键(在win10下)
  6. 牛客网剑指offer java 全部题解
  7. Tornado长轮询和WebSocket
  8. git 拉取远程分支报错(fatal: &#39;&#39; is not a commit and a branch &#39;&#39; cannot be created from it)
  9. 转载:sql用逗号连接多张表对应哪个join?
  10. [日常] nginx与location规则