UIButton的Touch事件

作者:暗夜星辰 发布于:2013-10-28 18:24 Monday 分类:IOS学习笔记

首先,UIControlEvents有这个几种:
UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside     = 1 <<  2,
UIControlEventTouchDragOutside    = 1 <<  3,
UIControlEventTouchDragEnter      = 1 <<  4,
UIControlEventTouchDragExit       = 1 <<  5,
UIControlEventTouchUpInside       = 1 <<  6,
UIControlEventTouchUpOutside      = 1 <<  7,
UIControlEventTouchCancel         = 1 <<  8,
---------------------------------------------------------------------------------------------------UIControlEventValueChanged        = 1 << 12,     // sliders, etc.
UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
UIControlEventEditingChanged      = 1 << 17,
UIControlEventEditingDidEnd       = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing
UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events
UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField
UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use

UIControlEventAllEvents           = 0xFFFFFFFF



http://blog.163.com/cz_jdton/blog/static/92732504201282543017312/
在这个网友的日志里看到一些内容,大体如下:
UIControlEventTouchDown
单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
当一次触摸从控件窗口内部拖动到外部时。
 
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents

通知所有事件。



为了验证下,做了一个实验:



//
//  ViewController.m
//  UIControlEvents
//
//  Created by WeiZhen_Liu on 13-4-27.
//  Copyright (c) 2013年 WeiZhen_Liu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(10, 100, 300, 40)];
    [button setTitle:@"Test" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(TouchDown:) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(TouchDownRepeat:) forControlEvents:UIControlEventTouchDownRepeat];
    [button addTarget:self action:@selector(TouchDragInside:) forControlEvents:UIControlEventTouchDragInside];
    [button addTarget:self action:@selector(TouchDragOutside:) forControlEvents:UIControlEventTouchDragOutside];
    [button addTarget:self action:@selector(TouchDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
    [button addTarget:self action:@selector(TouchDragExit:) forControlEvents:UIControlEventTouchDragExit];
    [button addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(TouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
    [button addTarget:self action:@selector(TouchCancel:) forControlEvents:UIControlEventTouchCancel];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPress:)];
    longPress.minimumPressDuration = 3;
    [button addGestureRecognizer:longPress];
    [longPress release];
    [self.view addSubview:button];
}

- (void)TouchDown:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}


- (void)TouchDownRepeat:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchDragInside:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchDragOutside:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchDragEnter:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchDragExit:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchUpInside:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchUpOutside:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)TouchCancel:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
}

@end

/**
 * UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
 * UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
 * UIControlEventTouchDragInside     = 1 <<  2,
 * UIControlEventTouchDragOutside    = 1 <<  3,
 * UIControlEventTouchDragEnter      = 1 <<  4,   // 注:我在模拟器上找了好久没找到这个东西什么时候触法
 * UIControlEventTouchDragExit       = 1 <<  5,
 * UIControlEventTouchUpInside       = 1 <<  6,
 * UIControlEventTouchUpOutside      = 1 <<  7,
 * UIControlEventTouchCancel         = 1 <<  8,   
 * 按下松开,依次触发:   TouchDown:      TouchUpInside:
 * 按下并在按钮内部拖动最后松开,依次触发:  TouchDown:    TouchDragInside:(多次)    TouchUpInside:
 * 按着按钮拖动到按钮外不远处,依次触发: TouchDown:    TouchDragInside:(多次)    TouchUpInside:
 * 按着按钮拖动到按钮外很远处,依次触发: TouchDown:    TouchDragInside:(多次)    TouchDragExit:    TouchDragOutside:(多次)    TouchUpOutside:
 * 快速双击两下,依次触发: TouchDown:    TouchUpInside:    TouchDown:    TouchDownRepeat:    TouchUpInside:
 * 快速击三下,依次触发: TouchDown:    TouchUpInside:    TouchDown:    TouchDownRepeat:    TouchUpInside:    TouchDown:    TouchDownRepeat:    TouchUpInside:
 * 长按会依次触发: TouchDown:    buttonLongPress:    TouchCancel:
 */
------------------------------------------------------------------------------------------------------------


说到UIControlEvent,再谈下UIControl, UIControl继承于UIView, 可以接收事件响应,看Demo:


- (void)viewDidLoad
{
    [super viewDidLoad];
    // UIControl : UIView
    UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    control.backgroundColor = [UIColor blueColor];
    [control addTarget:self action:@selector(controlViewClick_1) forControlEvents:UIControlEventTouchUpInside];
    [control addTarget:self action:@selector(controlViewClick_2) forControlEvents:UIControlEventTouchUpInside];
    [control addTarget:self action:@selector(controlViewClick_3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:control];
    [control release];
}

- (void)controlViewClick_1
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)controlViewClick_2
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)controlViewClick_3
{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}



程序打印:

2013-05-01 12:36:28.119 UIControlEventsTestDemo[1152:c07] controlViewClick_1


2013-05-01 12:36:28.122 UIControlEventsTestDemo[1152:c07] controlViewClick_2

2013-05-01 12:36:28.122 UIControlEventsTestDemo[1152:c07] controlViewClick_3



标签: App IOS UIControlEventTouch UIButton

发表评论:

©2012-2014 woodbunny.com 木头♥兔子的小屋 sitemap