UIEvent

  • UIEvent : 事件
  • 分三类: 触摸 晃动 远程遥控

UITouch

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸开始");

 //  点击空白处, 回收键盘
    [self.myTextField resignFirstResponder];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸移动");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸结束");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸被取消");
}

晃动

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
  // 摇一摇 随机更换背景颜色
 self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0  green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

    NSLog(@"晃动开始");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"晃动结束");
}

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

  • UIResponder 响应者类
  • iOS中,所有能响应事件(触摸, 晃动, 远程事件)的对象都是响应者
  • 系统定义了一个抽象的父类UIResponder来表示响应者, 其子类都是响应者

例:

1.点击一个继承于UIButton类的button按钮,实现让文本框弹出键盘的操作

 (void)click:(UIButton *)button{
    [self.myTextField becomeFirstResponder]; //第一响应者
}

2.创建一个继承于UIView 可以随着触摸移动的myView, 并且点击myView, 随机更换myView颜色

// 1.创建一个UIView类文件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//  随机生成背景颜色
   self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0  green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

// 集合里的元素个数
    NSLog(@"%ld", touches.count);



// 集合里有一个触摸类的对象
    UITouch *touch = [touches anyObject];
// 通过触摸对象获取相应的视图的当前位置
    self.startPoint = [touch locationInView:self];
    NSLog(@"%g", self.startPoint.x);

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 通过移动,找到变换,然后让MyView也进行相应的调整, 从而实现视图随手移动的效果
    // 获取触摸的对象
    UITouch *touch = [touches anyObject];
    // 获取移动之后的坐标
    CGPoint movePonit = [touch locationInView:self];
    // 坐标的变化
    CGFloat dx = movePonit.x - self.startPoint.x;
    CGFloat dy = movePonit.y - self.startPoint.y;
    // 设置视图的移动变化
    self.center = CGPointMake(self.center.x + dx, self.center.y + dy);
}

// 2.在视图控制器中创建myView对象, 实现该功能
 MyView *myView = [[MyView alloc]initWithFrame:CGRectMake(100, 150, 150, 90)];
    myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:myView];
    [myView release];

3.有关键盘上return, 用如下方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// 1.点击return, 回收键盘
[self.textField resignFirstResponder ];

// 2.点击return, 换行, 最后一个 回收键盘
 UITextField *myTextField1 = (UITextField *)[self.view viewWithTag:1001];
    UITextField *myTextField2 = (UITextField *)[self.view viewWithTag:1002];
    UITextField *myTextField3 = (UITextField *)[self.view viewWithTag:1003];
    UITextField *myTextField4 = (UITextField *)[self.view viewWithTag:1004];
    UITextField *myTextField5 = (UITextField *)[self.view viewWithTag:1005];

    if ([myTextField1 isFirstResponder]) {
        [myTextField2 becomeFirstResponder];
    }else if ([myTextField2 isFirstResponder]){
        [myTextField3 becomeFirstResponder];
    }else if ([myTextField3 isFirstResponder]){
        [myTextField4 becomeFirstResponder];
    }else if ([myTextField4 isFirstResponder]){
    [myTextField5 becomeFirstResponder ];
    }else{
    [myTextField5 resignFirstResponder];
    }


    return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值