我想要一个 UIView 来阻止响应手势的 subview 。主要目的是在服务器中验证数据时阻止用户界面的触摸和手势。处理请求后,它将从顶部移除阻塞 UIView。
如何做到这一点?到目前为止,我有以下内容:
@interface TableViewController ()
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (strong, nonatomic) IBOutlet UIView *blockingScreen;
....
@end
实现:
@implementation PlayListViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_table.dataSource = self;
_table.delegate = self;
// Blocking screen should take all the screen.
// Unfortunately, when it is displayed it gets the table size , ??
_blockingScreen = [[UIView alloc] initWithFrame:CGRectMake(0,0,
[[UIScreen mainScreen] applicationFrame].size.width,
[[UIScreen mainScreen], applicationFrame].size.height)];
_blockingScreen.userInteractionEnabled = NO;
[self loadGestureRecognizer];
}
- (void) loadGestureRecognizer
{
// Long press gesture recognizer declaration
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
// Swipe gesture recognizer declaration
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
// Attach gesture recognizers
[_songsTable addGestureRecognizer:longPress];
[_songsTable addGestureRecognizer:swipeLeftGesture];
}
// At some point, the gesture recognizer will be invoked
// In this case when swiping a table row
- (IBAction)swipeGestureRecognizer:(UISwipeGestureRecognizer *)sender
{
// Do things
...
// Preparing for doing Http request
// I want to display the blockingScreen on top of all views
// so no gestures are recognized.
[_table addSubview:_blockingScreen];
[_blockingScreen setBackgroundColor:[UIColor blackColor]];
_blockingScreen.alpha = 0.3;
// Bring to front
[_table bringSubviewToFront:_blockingScreen];
// To avoid gesture recognition I'm forced to do the following
_table.userInteractionEnabled = NO;
}
@end
在某些时候,滑动手势将调用 swipeGestureRecognizer。那很好用。 blockingScreen 似乎位于表格 View 的顶部(仅视为表格)并且不会阻止任何手势。
我是 objective-c 和 xcode 的新手,所以欢迎提供解释、教程或外部资源。我已经在 SO 中阅读了一些其他答案,例如 How to disable touch input to all views except the top-most view? , Container View Receives Touches Instead of Subview on iPad并尝试:
_blockingScreen.userInteractionEnabled = NO;
也为了
_blockingScreen.userInteractionEnabled = YES;
并没有发现任何区别。
有人知道吗?
最佳答案
这可以通过在执行处理时忽略交互事件来更容易地完成。
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
// Do all the things
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
更多信息在 documentation .
关于ios - Objective-C - 在验证数据时使用 subview 阻止触摸或手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29144813/