- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event是捕获手指的触摸事件方法,触摸开始时调用。
UITouch *touch = [[event allTouches] anyObject];touch定义所有事件中的任何触摸对象。
NSMutableSet *mutableTouches=[touches mutableCopy];这是另一种获取touches参数事件的方法
通过[allTouches count]来判断是多触点还是单触点
UITouch *touch1 =[[allTouches allObjects]objectAtIndex:0]当objectAtIndex为0时,获取第一个触摸点。
UITouch *touch2 =[[allTouches allObjects]objectAtIndex:1]当objectAtIndex为1时,获取第二个触摸点。 [touch tapCount]获得当前touch的判断是否为多点触摸,当[touch tapCount]=1时,判断为不是多点触摸。
这一章是要做一个单位转换器,主要用到了对键盘的处理,单击键盘上的return进行转换
//捕获手指的触摸事件方法,触摸开始时调用touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//touch定义所有事件中的任何触摸对象
UITouch *touch = [[event allTouches] anyObject];
//当手指在屏幕的触摸次数>=1时
if (touch.tapCount >= 1) {
//隐藏文本框输入的键盘
[chi resignFirstResponder];
[meter resignFirstResponder];
[feet resignFirstResponder];
}
}
//当用户触摸文本框时,键盘打开,当单击Return返回键时,键盘自动隐藏,返回单位转换计算结果
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//当文本框输入完成返回时
if (textField == chi) {
//隐藏键盘
[chi resignFirstResponder];
//建立浮点变量,计算后转换为米
double m = [chi.text doubleValue]/3.0003;
//计算后转换为英尺
double f = m * 3.2808;
meter.text = [[NSString alloc]initWithFormat:@"%.4f", m];
feet.text = [[NSString alloc]initWithFormat:@"%.4f", f];
}
if (textField == meter) {
[meter resignFirstResponder];
double c = [meter.text doubleValue] *3.0003;
double f = [meter.text doubleValue] *3.2808;
chi.text = [[NSString alloc]initWithFormat:@"%.4f", c];
feet.text = [[NSString alloc]initWithFormat:@"%.4f", f];
}
if (textField == feet) {
[feet resignFirstResponder];
double m = [feet.text doubleValue]/3.2808;
double c = m *3.0003;
meter.text = [[NSString alloc]initWithFormat:@"%.4f", m];
chi.text = [[NSString alloc]initWithFormat:@"%.4f", c];
}
return YES;
}