{
UIAlertView *alert;
UITapGestureRecognizer *recognizerTap;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self action:@selector(actionButton) forControlEvents:(UIControlEventTouchUpInside)];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
}
- (void)actionButton
{
alert = [[UIAlertView alloc] initWithTitle:@"模态测试"
message:@"请点击四周的模态区域我就消失"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTapBehind:)];
[recognizerTap setNumberOfTapsRequired:1];
recognizerTap.cancelsTouchesInView = NO;
[alert.window addGestureRecognizer:recognizerTap];
}
- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint location = [sender locationInView:nil];
if (![alert pointInside:[alert convertPoint:location fromView:alert.window] withEvent:nil]) {
[alert.window removeGestureRecognizer:sender];
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
}
}
本文介绍了一个iOS应用中实现按钮点击触发模态视图,并通过手势识别实现模态视图外部区域点击关闭的功能。文章展示了如何使用UIAlertView创建模态提示框,并通过UITapGestureRecognizer实现触摸穿透效果。
757

被折叠的 条评论
为什么被折叠?



