码迷,mamicode.com
首页 > 其他好文 > 详细

button不在父试图中,但是需要响应点击事件

时间:2016-06-27 17:06:56      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

当button不在父试图的范围内时,是无法响应点击事件的。项目中涉及了这个部分,之后写了个小demo(点击按钮,向上弹出两个button,并且都能响应点击事件),如下:

自定义view:CUMoreView

//
//  CUMoreView.h
//

#import <UIKit/UIKit.h>

typedef void(^btnClickBlock)(UIButton *btn);

@interface CUMoreView : UIView

@property (nonatomic, copy) btnClickBlock  btnClock;

- (void)didClickButtonWithBlock:(btnClickBlock)btnClock;

@end

 

//
//  CUMoreView.m
//  animation
//

#import "CUMoreView.h"
#import "UIView+CU.h"

@interface CUMoreView()
@property (nonatomic, weak) UIButton *btn2;
@property (nonatomic, weak) UIButton *btn3;
@property (nonatomic, assign) BOOL isShow;
@end

@implementation CUMoreView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.isShow = NO;
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    btn1.backgroundColor = [UIColor lightGrayColor];
    [btn1 setTitle:@"aaaa" forState:UIControlStateNormal];
    btn1.tag = 10001;
    [btn1 addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn1];
    
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, -30, 60, 30)];
    btn2.backgroundColor = [UIColor lightGrayColor];
    [btn2 setTitle:@"bbbb" forState:UIControlStateNormal];
    btn2.userInteractionEnabled = YES;
    btn2.alpha = 1;
    btn2.tag = 10002;
    [btn2 addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    self.btn2 = btn2;
    [self addSubview:btn2];
    
    UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(0, -60, 60, 30)];
    btn3.backgroundColor = [UIColor lightGrayColor];
    [btn3 setTitle:@"cccc" forState:UIControlStateNormal];
    btn3.alpha = 1;
    btn3.tag = 10003;
    [btn3 addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    self.btn3 = btn3;
    [self addSubview:btn3];
    return self;
}

-(void)didClickButton:(UIButton *)clickButton {
    NSInteger tag = clickButton.tag;

    if (tag == 10001) {
        
        if (self.isShow == NO) {//展开
            
      [UIView animateWithDuration:0.25 animations:^{
          self.btn2.y = -30;
          self.btn3.y = - 60;
          self.btn2.alpha = 1;
          self.btn3.alpha = 1;
          self.isShow = YES;
      }];
        }else{//收缩
          [UIView animateWithDuration:0.25 animations:^{
              self.btn3.y = 0;
              self.btn3.alpha = 0;
              self.btn2.y = 0 ;
              self.btn2.alpha = 0;
              self.isShow = NO;
          }];
        }
    }
    
    else {
    
    if (self.btnClock) {
        self.btnClock(clickButton);
    }
    }
}

-(void)didClickButtonWithBlock:(btnClickBlock)btnClock {
    if (btnClock) {
        self.btnClock = btnClock;
    }
    
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        CGPoint tempoint = [self.btn2 convertPoint:point fromView:self];
        CGPoint tempoint2 = [self.btn3 convertPoint:point fromView:self];
        
        if (CGRectContainsPoint(self.btn2.bounds, tempoint))
        {
            view = self.btn2;
        }
        
        if (CGRectContainsPoint(self.btn3.bounds, tempoint2)) {
            view = self.btn3;
        }
        
    }
    return view;
}

@end

 

//
//  ViewController.m
//

#import "ViewController.h"
#import "CUMoreView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CUMoreView *moreView = [[CUMoreView alloc]initWithFrame:CGRectMake(200, 200, 60, 120)];
    [moreView didClickButtonWithBlock:^(UIButton *btn) {
        if (btn.tag == 10002) {
            
            NSLog(@"点击了中间的按钮");
            
        }else if (btn.tag == 10003) {
             NSLog(@"点击了上面的按钮");
        }
    }];
    
    [self.view addSubview:moreView];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

button不在父试图中,但是需要响应点击事件

标签:

原文地址:http://www.cnblogs.com/dsp-ios/p/5620534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!