事件处理

Touch

下面4个方法是UIResponder的方法 继承于UIResponder的类都可以重写此方法

// 开始触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}
// 触摸时移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}
// 触摸结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}
// 触摸中断
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 如来电话 小退出
}

在开始触摸时 可以获取当前触摸点 和 前一个触摸点 相对于被触摸视图的坐标:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"开始摸...");
// UITouch 保存手指信息(触摸的点)
    UITouch *touch = [touches anyObject];
//    NSLog(@"%@",touch);

// 取出当前触摸的点
// 返回一个当前触摸的点 相对于传进去的参数View
    CGPoint p1 = [touch locationInView:self];
    NSLog(@"%@",NSStringFromCGPoint(p1));

// 返回当前点的上一个点 相对于传进去的参数View
    CGPoint p2 = [touch previousLocationInView:self];
    NSLog(@"%@",NSStringFromCGPoint(p2));

在触摸时移动的方法中 可以让视图移动

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"抚摸中...");
// 取出当前点
// 取出当前点上一个点
// 计算X轴的偏移量
// 计算Y轴的偏移量
// 改变视图原来的中心点
    UITouch *touch = [touches anyObject];
    CGPoint p1 = [touch locationInView:self];
    CGPoint p2 = [touch previousLocationInView:self];
    CGFloat x = p1.x - p2.x;
    CGFloat y = p1.y - p2.y;
    self.center = CGPointMake(self.center.x + x, self.center.y + y);

// 随机变颜色
    self.backgroundColor = [UIColor colorWithRed:arc4random()% 256/255.0 green:arc4random()% 256/255.0 blue:arc4random()% 256/255.0 alpha:1];
}

响应者链

响应者链 分为两个过程
1.查询过程

当你点击屏幕 先定位到应用程序->window->控制器->self.view->view的子视图一一进行查找 直到定位到被点击的子视图 查询过程结束

2.响应过程
首先 看本视图能不能处理事件(实现了touchBegan等方法 就叫做可以处理事件)->父视图->一层一层往下看能不能处理 直到window 如果都不能处理 该次点击事件 被遗弃(无效点击)

注意: UILabel UIImageView的交互 默认时关闭的

创建一个button 测试响应者链:

当button的交互属性被关闭时 点击按钮无法触发方法

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(200, 200, 100, 100);
    button.backgroundColor = [UIColor brownColor];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    // 关闭button交互 (阻断查询过程)
    button.userInteractionEnabled = NO;
    [self.view addSubview:button];

Motion

下面4个方法是UIResponder的方法 继承于UIResponder的类都可以重写此方法

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"开始晃动");
}

// 在结束晃动方法中 可以实现界面跳转
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"结束晃动");

// 跳转界面
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self presentViewController:secondVC animated:YES completion:nil];
    [secondVC release];

}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"中断晃动");
}

如果要想通过晃动跳转回原来的视图 ,可以在当前视图的控制器中实现方法:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值