iOS开发 -- 事件处理(触摸,晃动)

iOS中主要包括三种事件 : 触摸事件 , 晃动事件 , 远程控制事件
1:
触摸事件 : 用户用手指触摸设备硬件 发生的事件对象
对于我们的UIView控件,都是可以对触摸事件进行响应 之前之所以没有响应 是因为我们没有实现 touchesBrgan touchesMoved touchesEnded touchesCanclled方法
方法代码:
//开始触摸 当手指触摸到屏幕时候触发

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self changeColorofSelf];
    [self changgePosition];
     [self changgeSize];
    NSLog(@"啊啊啊啊啊~~~");
}

//移动触摸 当手指在屏幕上滑动时候 触发

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self changeColorofSelf];
    [self changgePosition];
    [self changgeSize];
    NSLog(@"好舒服啊~~~");
}

//结束触摸 当手指离开屏幕的时候触发

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"不要停");
}

//取消触摸 当手指仍然在屏幕上还未离开 此时应用程序进入后台时候触发 (电话打入)

 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"我还要");
}

//改变颜色

-(void)changeColorofSelf
{
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
   self.superview.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
//改变位置
- (void)changgePosition
{
    self.center = CGPointMake(arc4random()%(325 - 100 +1)+100, arc4random()%(400-100+1)+100);
}
- (void)changgeSize
{
    self.bounds = CGRectMake(0, 0, arc4random()%(275 - 100 +1)+100, arc4random()%(200-100+1)+100);
}

2:
晃动事件:

//开始触摸 当手指触摸到屏幕时候触发

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取手指
    UITouch *touch = [touches anyObject];
    //获取手指在当前视图上的位置
    self.touchPosition = [touch locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(_touchPosition));
}

//移动触摸  当手指在屏幕上滑动时候 触发
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"好舒服啊~~~");
    //先获取手指'
    UITouch *touch = [touches anyObject];
    //获取手指在视图上的位置
    CGPoint position = [touch locationInView:self];
    //获取水平方向和竖直方向的差额
    CGFloat x = position.x - _touchPosition.x;
    CGFloat y = position.y - _touchPosition.y;
    //移动视图
    self.center = CGPointMake(self.center.x+x, self.center.y+y);
}

3:
响应者链 : 由多个响应者组成的链状结构
对于响应 有两个过程,响应事件查询的过程 和 响应事件的处理过程
查询过程 : 硬件设备–> Application –> window –> 视图控制器 –> View –> 视图的各个子视图 最终照找到被触摸的视图
处理过程 : 被触摸视图 –> 传给父视图 –> View –> 视图控制器 –>window –>application …遗失

关闭自身 以及 子视图的用户交互 此时响应者链 被打破 , 对于视图以及子视图不会再对用户做出响应
默认是打开的

   self.view.userInteractionEnabled = YES; //NO
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值