首页 > 移动平台 > 详细

iOS button被view遮住,想点击怎么破

时间:2015-07-01 17:23:11      阅读:252      评论:0      收藏:0      [点我收藏+]

UIView的提供了这个方法:- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;

1.创建CustomView继承自UIView :

CustomView.h

@property (strong,nonatomic)NSArray * passthroughViews;

@property (nonatomic)BOOL testHits;

 -(BOOL) isPassthroughView: (UIView*) view;

CustomView.m

-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event{

    if(self.testHits){

        return nil;

    }    

    if(!self.passthroughViews

       || (self.passthroughViews && self.passthroughViews.count == 0)){

        return self;

    } else {

        UIView *hitView = [super hitTest:point withEvent:event];

        if (hitView == self) {

            self.testHits = YES;

            CGPoint superPoint = [self.superview convertPoint:point fromView:self];

            UIView *superHitView = [self.superview hitTest:superPoint withEvent:event];

            self.testHits = NO;

            if ([self isPassthroughView:superHitView]) {

                hitView = superHitView;

            }

        }

        return hitView;

    }

}

- (BOOL)isPassthroughView:(UIView *)view {

    if (view == nil) { 

        return NO;

    }

    if ([self.passthroughViews containsObject:view]) {

        return YES;

    }

    return [self isPassthroughView:view.superview];

}

 

2.创建ViewController继承自UIViewController :

ViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];

    self.passthroughButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [self.passthroughButton setTitle:@"Passthrough" forState:UIControlStateNormal];

    [self.view addSubview:self.passthroughButton];

    self.passthroughButton.frame = CGRectMake(20, 50, 120, 28);

    [self.passthroughButton addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];

 

    CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(80, 10, 300, 200)];

    customView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.5];

    //customView.passthroughViews = [NSArray arrayWithObject:self.passthroughButton];

    [self.view addSubview:customView];

    

}

 

iOS button被view遮住,想点击怎么破

原文:http://www.cnblogs.com/tongyuling/p/4613406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!