UITextFiled,UIButton,UIImageView交互相互之间的事件拦截

UIButton右上方添加一个笑button如:

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame= CGRectMake(100, 400, 120, 30);
    [self.view addSubview:button];
    UIButton * btn1 = [[UIButton alloc]initWithFrame:CGRectMake(button.frame.size.width-10, -10, 20, 20)];
    [btn1 setImage:[UIImage imageNamed:@"ic_login_weixin"] forState:UIControlStateNormal];
    [button addSubview:btn1];
    button.layer.borderWidth = 1.f;
    button.layer.borderColor = [UIColor blackColor].CGColor;
    [btn1 addTarget:self action:@selector(touches) forControlEvents:UIControlEventTouchUpInside];虽然右上方的but1可以完全显示出来,但是点击btn1不能进行交互,也就是不能触发touches函数。

 

同理,在UITextFiled的右上方添加一个button如:

UITextField * filed = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 120, 30)];
    [self.view addSubview:filed];
    filed.borderStyle = UITextBorderStyleRoundedRect;
    filed.layer.masksToBounds = NO;
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(filed.frame.size.width-5, -5, 10, 10)];
    [filed addSubview:btn];
    btn.backgroundColor = [UIColor redColor];
    self.view.backgroundColor = [UIColor whiteColor];

[btn addTarget:self action:@selector(touches) forControlEvents:UIControlEventTouchUpInside];

点击右上方的btn同样是不能进行交互的,也就是说该事件被filed给拦截了。

实验,将

filed.userInteractionEnabled = NO;

btn.userInteractionEnabled = YES;

同样也会产生冲突不能进行交互。

 

但是!!!注意!!!但是!!!

实验UIImageView上面添加btn,然后让imageView可以交互,就能完成右上方的小btn点击时间啦!!!

代码如下:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
    UIButton* delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    delBtn.tag = 101;
    delBtn.frame = CGRectMake(imageView.frame.size.width-20, -10, 30, 30);
    [delBtn setImage:[UIImage imageNamed:@"ic_login_weixin"] forState:UIControlStateNormal];
    [delBtn addTarget:self action:@selector(delBtntouches) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:delBtn];
    [self.view addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"addimage"];
    imageView.backgroundColor = [UIColor redColor];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTouched)];
    [imageView addGestureRecognizer:ges];
    imageView.userInteractionEnabled= YES;

而且非常有意思的是,当点击除了delBtn的imageView的其它部分,响应的都是imageViewTouched,而点击了delBtn,则相应的是delBtntouches,但是delBtn超出imageview的部分点击也是没有效的。

 

由此我们可以推论出:uilabel已定也是可以这样来进行小btn的添加

 

最后:该理论的实际应用:

我们可以 在原来的图像上自定义来写一些图片增删的功能了。

 

 

 

  

 

转载于:https://www.cnblogs.com/fusheng-it/p/5359404.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值