码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-07-01 17:26:22      阅读:334      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!