9.UIEvent

参考网络上其他作者做的总结归纳   用于方便学习翻看

三种类型事件

响应者对象

响应者对象是可以响应事件并对其进行处理的对象。

UIResponder是所有响应者对象的基类,它不仅为事件处理,而且也为常见的响应者行为定义编程接口。

UIApplication、UIView、和所有从UIView派生出来的UIKit类(包括UIWindow)都直接或间接地继承自UIResponder类。


响应者链

响应链是一个响应者对象的连接序列,事件或动作消息(或菜单编辑消息)依次传递。它允许响应者对象把事件处理的职责转交给其它更高层的对象。应用程序通过向上传递一个事件来查找合适的处理对象。因为点击检测视图也是一个响应者对象,应用程序在处理触摸事件时也可以利用响应链。响应链由一系列的下一个响应者组成。

响应者链处理原则

1. 点击检测视图或者第一响应者传递事件或动作消息给它的视图控制器(如果它有的话);如果没有一个视图控制器,就传递给它的父视图。

2. 如果一个视图或者它的视图控制器不能处理这个事件或动作消息,它将传递给该视图的父视图。

3. 在这个视图层次中的每个后续的父视图遵循上述的模式,如果它不能处理这个事件或动作消息的话。

4. 最顶层的视图如果不能处理这个事件或动作消息,就传递给UIWindow对象来处理。

5. 如果UIWindow 对象不能处理,就传给单件应用程序对象UIApplication。

如果应用程序对象也不能处理这个事件或动作消息,将抛弃它。


1.触摸事件

事件处理方法

在给定的触摸阶段中,如果发生新的触摸动作或已有的触摸动作发生变化,应用程序就会发送这些消息:

当一个或多个手指触碰屏幕时,发送touchesBegan:withEvent:消息。

当一个或多个手指在屏幕上移动时,发送touchesMoved:withEvent:消息。

当一个或多个手指离开屏幕时,发送touchesEnded:withEvent:消息。

当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:withEvent:消息。

-------------------实例-----------------------

touch.phase,触摸事件的阶段。

touch.tapCount,触摸事件的轻碰次数,可以判断双击事件。

UIEvent 的allTouches方法,可以获得触摸点的集合,可以判断多点触摸事件。

复制代码
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesMoved - touch count = %d", [touches count]);
    for(UITouch *touch in event.allTouches) {
        [self logTouchInfo:touch];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesEnded - touch count = %d", [touches count]);
    for(UITouch *touch in event.allTouches) {
        [self logTouchInfo:touch];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesCancelled - touch count = %d", [touches count]);
    for(UITouch *touch in event.allTouches) {
        [self logTouchInfo:touch];
    }
}

不接受处理事件的三种方法
  1. 不接收用户交互:userInteractionEnabled = NO;
  2. 隐藏:hidden = YES;
  3. 透明:alpha = 0~0.01

2.运动事件

摇一摇

在 UIResponder中存在这么一套方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);


你只需要让这个Controller本身支持摇动
同时让他成为第一相应者:
- (void)viewDidLoad
{
     [superviewDidLoad];
// Do any additional setup after loading the view, typically from anib.
     [[UIApplicationsharedApplication]setApplicationSupportsShakeToEdit:YES];
[self
becomeFirstResponder];
}
然后去实现那几个方法就可以了
- (void) motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent
*)event
{
     //检测到摇动
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
     //摇动取消
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event

{
     //摇动结束
     if(event.subtype == UIEventSubtypeMotionShake) {
         //somethinghappens

     }
}


重力感应


UIAccelerometer加速计是用来检测iphone手机在x.y.z轴三个轴上的加速度。要获得此类调用:

UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
同时,你需要设置它的delegate。
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = 1.0/60.0;
委托方法:- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration是表示加速度类。包含了来自加速计UIAccelerometer的真是数据。它有3个属性的值x、y、z。iphone的加速计支持最高以每秒100次的频率进行轮询。此时是60次。
1) 应用程序可以通过加速计来检测摇动,如:用户可以通过摇动iphone擦除绘图。
也可以用户连续摇动几次iphone,执行一些特殊的代码:
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
static NSInteger shakeCount = 0;
static NSDate *shakeStart;
NSDate *now = [[NSDate alloc] init];
NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];
if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)
{
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
[now release];
[checkDate release];
if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)
{
shakeCount++;
if (shakeCount > 4)
{
// -- DO Something
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
}
}
2) 加速计最常见的是用作游戏控制器。在游戏中使用加速计控制对象的移动! 在简单情况下,可能只需获取一个轴的值,乘上某个数(灵敏度),然后添加到所控制对象的坐标系中。在复杂的游戏中,因为所建立的物理模型更加真实,所以必须 根据加速计返回的值调整所控制对象的速度


在cocos2d中接收加速计输入input.使其平滑运动,一般不会去直接改变对象的position.通过:
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// -- controls how quickly velocity decelerates(lower = quicker to change direction)
float deceleration = 0.4;
// -- determins how sensitive the accelerometer reacts(higher = more sensitive)
float sensitivity = 6.0;
// -- how fast the velocity can be at most
float maxVelocity = 100;
// adjust velocity based on current accelerometer acceleration
playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
// -- we must limit the maximum velocity of the player sprite, in both directions
if (playerVelocity.x > maxVelocity)
{
playerVelocity.x = maxVelocity;
}
else if (playerVelocity.x < - maxVelocity)
{
playerVelocity.x = - maxVelocity;
}
}
上面deceleration是减速的比率,sensitivity是灵敏度。maxVelocity是最大速度,如果不限制则一直加大就很难停下来。

playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
中 playervelocity是一个速度向量。是累积的。
- (void) update: (ccTime)delta
{
// -- keep adding up the playerVelocity to the player's position
CGPoint pos = player.position;
pos.x += playerVelocity.x;
// -- The player should also be stopped from going outside the screen
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float imageWidthHalved = [player texture].contentSize.width * 0.5f;
float leftBorderLimit = imageWidthHalved;
float rightBorderLimit = screenSize.width - imageWidthHalved;
// -- preventing the player sprite from moving outside the screen
if (pos.x < leftBorderLimit)
{
pos.x = leftBorderLimit;
playerVelocity = CGPointZero;
}
else if (pos.x > rightBorderLimit)
{
pos.x = rightBorderLimit;
playerVelocity = CGPointZero;
}
// assigning the modified position back
player.position = pos;

}


转屏



3.远程控制事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值