UIControl 详细解释
2016-04-05 16:04
525 查看
第一 :UIControl 的继承。
UIControl 是继承与 UIView的。而 UIView继承与UIResponder的。然后 UIResponder 继承与 NSObject的。图形解释如下:第二:UIcontrol 的方法详细解释。
一,创建一个继承与 UIcontrol 的类。// ZSJButton.h
// 基础(一)
// Created by 周双建 on 16/4/5.
// Copyright © 2016年
周双建. All rights reserved.
#import <UIKit/UIKit.h>
@interface ZSJButton :
UIControl{
@public
//标题
UILabel * ZSJButtonTitle;
//图片
UIImageView * ZSJButtonImageView;
}
@end
二,该类的实现。
// ZSJButton.m
// 基础(一)
// Created by 周双建 on 16/4/5.
// Copyright © 2016年
周双建. All rights reserved.
#import "ZSJButton.h"
@implementation ZSJButton
-(instancetype)initWithFrame:(CGRect)frame{
if (self == [super
initWithFrame:frame]) {
self.backgroundColor = [[UIColor
whiteColor]colorWithAlphaComponent:1.0f];
self.layer.borderWidth =
0.5f;
self.layer.borderColor = [[UIColor
lightGrayColor]
CGColor];
self->ZSJButtonTitle = [[UILabel
alloc]initWithFrame:CGRectMake(4.0f,
4.0f, frame.size.width-8.0f, frame.size.height
- 8.0f)];
self->ZSJButtonTitle.text =
@"ZSJButton";
self->ZSJButtonTitle.textColor
= [[UIColor
blackColor] colorWithAlphaComponent:1.0f];
self->ZSJButtonTitle.font = [UIFont
systemFontOfSize:20.0f];
self->ZSJButtonTitle.textAlignment
= NSTextAlignmentCenter;
[self addSubview:self->ZSJButtonTitle];
}
return
self;
}
// 这是开始启用跟踪事件
-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
if (touch.timestamp ==
2.0f) {
// 设置点击多长时间,才触发事件
}
if (touch.tapCount ==
3) {
// 设置点击的次数,连续点击
}
return [super
beginTrackingWithTouch:touch withEvent:event];
}
// 这个是当控件被点击后并在空间上移动,所调用的方法(只要你点击,不放手一直移动,它就会一直调用这个方法,哪怕已经移除控件的大小。)
-(BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
/*
* CGRectInset
* CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)
* 该结构体的应用是以原rect为中心,再参考dx,dy,进行缩放或者放大。
*/
CGRect InRect =
CGRectInset(self.bounds, -30.f, -30.f);
/*
* CGRectContainsPoint
* CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>)
* 判断给定的点是否被一个CGRect包含
*/
BOOL InView = !CGRectContainsPoint(InRect, [touch
locationInView:self]);
if (InView) {
/*
* previousLocationInView
* 该方法记录了前一个坐标值,函数返回也是一个CGPoint类型的值
*/
BOOL OutView =
CGRectContainsPoint(InRect, [touch previousLocationInView:self]);
if (OutView) {
[self
sendAction:@selector(Tap:withEvent:)
to:self forEvent:event];
// you can write your code.(Condition or special function)
}else{
NSLog(@"我已经远离控件了");
// 传递所有的事件
[self
sendActionsForControlEvents:event];
// you can write your code.(Condition or special function)
}
}
return [super
continueTrackingWithTouch:touch withEvent:event];
}
-(void)Tap:(id)mytap withEvent:(UIEvent*)event{
NSLog(@"我被移动30px出去了");
}
// 这是跟踪事件的结束的时候,调用的方法
-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
/*
* 事件的类型
typedef NS_ENUM(NSInteger, UIEventType) {
*
手指事件
UIEventTypeTouches,
*
摇动手势
UIEventTypeMotion,
*
远程事件
UIEventTypeRemoteControl,
*
按压事件
UIEventTypePresses NS_ENUM_AVAILABLE_IOS(9_0),
};
*/
switch (event.type) {
case
UIEventTypeTouches:
{
//your codes
}
break;
case
UIEventTypeMotion:
{
//your codes
}
break;
case
UIEventTypeRemoteControl:
{
//your codes
}
break;
case
UIEventTypePresses:
{
//your codes
}
break;
default:
break;
}
// 这里可以处理远程事件
switch (event.subtype) {
/*
typedef NS_ENUM(NSInteger, UIEventSubtype) {
// 不包含任何子事件类型
UIEventSubtypeNone = 0,
// 摇晃事件(从iOS3.0开始支持此事件)
UIEventSubtypeMotionShake = 1,
//远程控制子事件类型(从iOS4.0开始支持远程控制事件)
//播放事件【操作:停止状态下,按耳机线控中间按钮一下】
UIEventSubtypeRemoteControlPlay = 100,
//暂停事件
UIEventSubtypeRemoteControlPause = 101,
//停止事件
UIEventSubtypeRemoteControlStop = 102,
//播放或暂停切换【操作:播放或暂停状态下,按耳机线控中间按钮一下】
UIEventSubtypeRemoteControlTogglePlayPause = 103,
//下一曲【操作:按耳机线控中间按钮两下】
UIEventSubtypeRemoteControlNextTrack = 104,
//上一曲【操作:按耳机线控中间按钮三下】
UIEventSubtypeRemoteControlPreviousTrack = 105,
//快退开始【操作:按耳机线控中间按钮三下不要松开】
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
//快退停止【操作:按耳机线控中间按钮三下到了快退的位置松开】
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
//快进开始【操作:按耳机线控中间按钮两下不要松开】
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
//快进停止【操作:按耳机线控中间按钮两下到了快进的位置松开】
UIEventSubtypeRemoteControlEndSeekingForward = 109,
};
*/
case
UIEventSubtypeNone:
{
// your codes
}
break;
case
UIEventSubtypeMotionShake:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlPlay:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlPause:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlStop:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlTogglePlayPause:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlNextTrack:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlPreviousTrack:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlBeginSeekingBackward:
{
// your codes
}
break;
case
UIEventSubtypeRemoteControlEndSeekingBackward:
{
// your codes
}
case
UIEventSubtypeRemoteControlBeginSeekingForward:
{
// your codes
}
case
UIEventSubtypeRemoteControlEndSeekingForward:
{
// your codes
}
break;
default:
break;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
三,该类的使用。
// ViewController.m
// 基础(一)
// Created by 周双建 on 16/4/5.
// Copyright © 2016年
周双建. All rights reserved.
#import "ViewController.h"
#import "ZSJButton.h"
@interface
ViewController ()
@property(nonatomic,assign)
ZSJButton * MyBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
ZSJButton * Btn = [[ZSJButton
alloc]initWithFrame:CGRectMake(100,
100, 100,
50)];
_MyBtn = Btn;
// enabled 是控制控件是否可用或者从新绘制等,默认是 YES
Btn.enabled =
YES;
// 获取控件是否可用的状态,只读特性
NSLog(@"
按钮的状态:%d",Btn.isEnabled);
// selected
是设置控件的选中或者不选中
Btn.selected =
YES;
//这是控件是否进行事件的跟踪
//
这是 UIControl方法
给控件添加点击方法
[Btn addTarget:self
action:@selector(ButClick:)
forControlEvents:UIControlEventTouchUpInside];
[Btn addTarget:self
action:@selector(ButClick1:)
forControlEvents:UIControlEventTouchUpInside];
[Btn addTarget:self
action:@selector(ButClick2)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:Btn];
//
获取控件上所有的 Target
NSLog(@"AA:%@",[Btn
allTargets]);
// 获取
指定 tager 上的
所有同一类事件的所有方法
/*
2016-04-05 14:40:01.470
基础(一)[3622:266503] BB:(
"ButClick:",
"ButClick1:",
ButClick2
)
从输出的结果看出。方法的写法不同,输出的结果也是不同的。
*/
NSLog(@"BB:%@",[Btn
actionsForTarget:self
forControlEvent:UIControlEventTouchUpInside]);
// Do any additional setup after loading the view, typically from a nib.
}
-(void)ButClick:(UIButton*)Btn{
_MyBtn->ZSJButtonTitle.text =
@"Tap_One";
_MyBtn.highlighted =
YES;
// 这个方法是移除,添加的一些方法
[_MyBtn
removeTarget:self
action:@selector(ButClick:)
forControlEvents:UIControlEventTouchUpInside];
NSLog(@"
按钮是否开启跟踪状态:%d",Btn.isTracking);
}
-(void)cancelTrackingWithEvent:(UIEvent *)event{
}
-(void)ButClick1:(id) temp{
}
-(void)ButClick2{
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- PostgreSQL教程(三):表的继承和分区表详解
- Lua面向对象之类和继承浅析
- 浅析Ruby中继承和消息的相关知识
- 设计引导--一个鸭子游戏引发的设计理念(多态,继承,抽象,接口,策略者模式)
- C++实现不能被继承的类实例分析
- C# 面向对象三大特性:封装、继承、多态
- PHP类的封装与继承详解
- js继承 Base类的源码解析
- Javascript 面向对象 继承
- JavaScript 继承使用分析
- Javascript面向对象编程(二) 构造函数的继承
- Javascript中的几种继承方式对比分析
- javascript面向对象之Javascript 继承
- JavaScript 对象、函数和继承
- 详述JavaScript实现继承的几种方式(推荐)
- javascript 继承学习心得总结
- 学习javascript面向对象 javascript实现继承的方式
- 关于JavaScript的面向对象和继承有利新手学习
- JavaScript 继承详解(一)
- JavaScript 继承详解(二)