UITapGestureRecognizer的使用影响视图上某些控件的action的问题

我的界面上的视图层次是这样的:

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等时可以把我们触摸取消,使得控件正常使用。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值