此QA来自苹果开发者库,贴下地址:
https://developer.apple.com/library/ios/qa/qa2013/qa1812.html#//apple_ref/doc/uid/DTS40014336
故障原因:当控件在父视图的区域外,区域外的控件虽然可以显示出来,但是无法响应点击事件。
解决办法:继承父类,Override下 hitTest方法:代码如下:
// // OutMaskView.h // testOutsideHit // // Created by twb on 14-4-14. // Copyright (c) 2014年 twb. All rights reserved. // #import <UIKit/UIKit.h> @interface OutMaskView : UIView @property (nonatomic, strong) UIView *targetView; @end
//
// OutMaskView.m
// testOutsideHit
//
// Created by twb on 14-4-14.
// Copyright (c) 2014年 twb. All rights reserved.
//
#import "OutMaskView.h"
@implementation OutMaskView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// Convert the point to the target view‘s coordinate system.
// The target view isn‘t necessarily the immediate subview
CGPoint pointForTargetView = [self.targetView convertPoint:point fromView:self];
if (CGRectContainsPoint(self.targetView.bounds, pointForTargetView)) {
// The target view may have its view hierarchy,
// so call its hitTest method to return the right hit-test view
return [self.targetView hitTest:pointForTargetView withEvent:event];
}
return [super hitTest:point withEvent:event];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
viewDidLoad方法种设置您的目标视图,也即你不能接受事件的视图。
//
// ViewController.m
// testOutsideHit
//
// Created by twb on 14-4-14.
// Copyright (c) 2014年 twb. All rights reserved.
//
#import "ViewController.h"
#import "OutMaskView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *testButton;
@property (weak, nonatomic) IBOutlet OutMaskView *maskView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.maskView.targetView = self.testButton;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)testMask:(id)sender
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"信息" message:@"现在可以点击了!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[av show];
}
@end
原文:http://blog.csdn.net/airk000/article/details/23686685