我的界面上的视图层次是这样的:
scrollView=[[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.backgroundColor=[UIColor clearColor];
[self.view addSubview:scrollView];
[scrollView release];
userInfoView=[[UIView alloc] initWithFrame:CGRectMake(0, 84, 320, 376)];
userInfoView.backgroundColor=[UIColor clearColor];
userInfoView.userInteractionEnabled=YES;
[scrollView addSubview:userInfoView];
[userInfoView release];
UIButton *editButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
editButton.frame=CGRectMake(93, 248, 115, 37);
editButton.userInteractionEnabled=YES;
[editButton setTitle:@"修改用户信息" forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(changeUserInfo) forControlEvents:UIControlEventTouchUpInside];
[userInfoView addSubview:editButton];
UILabel *oldPassWordLabel=[[UILabel alloc] initWithFrame:CGRectMake(20, 53, 87, 20)];
oldPassWordLabel.text=@"原口令:";
oldPassWordLabel.textAlignment=UITextAlignmentLeft;
oldPassWordLabel.backgroundColor=[UIColor clearColor];
[passwordView addSubview:oldPassWordLabel];
[oldPassWordLabel release];
oldPassword=[[UITextField alloc] initWithFrame:CGRectMake(124, 48, 176,30)];
oldPassword.borderStyle=UITextBorderStyleRoundedRect;
oldPassword.clearButtonMode=UITextFieldViewModeWhileEditing;
oldPassword.text=@"";
oldPassword.delegate=self;
oldPassword.returnKeyType=UIReturnKeyNext;
//oldPassword.backgroundColor=[UIColor clearColor];
oldPassword.secureTextEntry=YES;
[passwordView addSubview:oldPassword];
[oldPassword release];
UITapGestureRecognizer* onTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
onTap.delegate = self;
[passwordView addGestureRecognizer:onTap];
[onTap release];
加上onTap之后,当我点击editButton的时候只调用handleSingleTap这个方法,不调用changeUserInfo这个方法,点击oldPassword输入内容喝点击clearButton清楚内容的时候也不好用,后来我加上这么一句话 onTap.cancelsTouchesInView=NO;然后点击按钮好用了,可是点击oldPassword还是不好用,最后发现这样可以解决这个问题:UIGestureRecognizerDelegate这个代理方法里面有这个- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if (oldPassword.superview!=nil) {
if ([touch.view isKindOfClass:[UIControl class]]) {
return NO;
}
}
return YES;
}这样就当我们点击UIControl子类的控件UIButton、UITextField、UISlider等时可以把我们触摸取消,使得控件正常使用。