button不在父试图中,但是需要响应点击事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了button不在父试图中,但是需要响应点击事件相关的知识,希望对你有一定的参考价值。
当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不在父试图中,但是需要响应点击事件的主要内容,如果未能解决你的问题,请参考以下文章