用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 ...
随机推荐
- java学习-get和post请求
摘要 看完本文可以知道,使用java原生编写get/post请求的步骤,进行网络请求时应该注意的地方. 这里使用java自带的HttpUrlConnection对象进行网络请求, 请求步骤大致分为五步 ...
- mysql数据库修改字符编码问题
遇到这种情况,现有项目的数据库已经建好,数据表也已经创建完成. 问题来的,数据库不能插入中文,调试时候发现中文数据从发送请求到最后请求处理完成这些步骤,中文还没有发生乱码. 只有在存储到数据库后查询数 ...
- 外网访问不了Xampp(本地访问不了虚拟机的Xampp)
安装好了Xampp,在虚拟机是可以访问的, 浏览器中输入localhost 嘛 不过在本地就是访问不了,ping是能通过的 然后网上查了一些资料,并结合Xampp的提示: 特别注意:如果连上面这 ...
- Nginx+Memcached+Tomcat集群配置
1. Nginx Nginx是通过将多个Web Server绑定到同一个IP地址下,以实现多个WebServer间的负载均衡,降低单个Web Server的负荷,以提高整体的性能与稳定性. 安装和 ...
- composer如何自动验证并获取gitlab的私有库?
近期购买了Laravel的nova以后,需要对它的核心代码做一些修改,为方便与团队其他成员分享,以及在nova官方库更新后方便对差异管理.便将nova库挂在自己的gitlab,通过compos ...
- [转]ASP.NET cache缓存的用法
本文转自:https://blog.csdn.net/mss359681091/article/details/51076712 本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpC ...
- C#神奇的扩展方法
以前总听说扩展方法扩展方法,只是听说是C#3.0后出来的新玩意,也仅仅是知道Linq中用到好多的扩展方法,但是他究竟是个什么东东,怎么用它,用它的好处是什么,总是模模糊糊,那么我们今天就尝试揭开它 ...
- Jsp&Servlet入门级项目全程实录第6讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.建立数据表及数据(略) 2.创建student model package com.java1234.model; ...
- java基础题目日常思考(持续更新)
public static void main(String[] args) { Integer a = 0; count(a); System.out.println(a); // 问题: a 输出 ...
- 进程间通信IPC -- 管道, 队列
进程间通信--IPC(Inter-Process Communication) 管道 from multiprocessing import Pipecon1,con2 = Pipe()管道是不安全的 ...