我想有一个UIView阻止子视图以响应手势。主要目的是在服务器中验证数据时阻止用户界面接触和手势。处理完请求后,它将从阻止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似乎位于表格视图的顶部(仅仅是表格)并且不会阻止任何手势。
我对objective-c和xcode很新,所以欢迎解释,教程或外部资源。我已经在其中阅读了其他一些答案,例如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;
并没有看到任何差异。
任何人都有任何想法?
答案 0 :(得分:1)
通过在执行处理时忽略交互事件,可以更轻松地完成此任务。
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
// Do all the things
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
documentation中的更多信息。
答案 1 :(得分:1)
来自docs:“一个窗口将触摸事件传递给手势识别器,然后将其传递到连接到手势识别器的经过测试的视图。”
所以......视图并没有真正禁用手势识别器
=>禁用它:regconizer.enabled = false
答案 2 :(得分:0)
你可以这样 首先,您必须管理isValidated或不使用,然后像这样使用 1.解决方案
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (isValidated)
{
return YES;
}else{
if(gestureRecognizer.view == songsTable) return NO;
}
return YES;
}
我希望它可以帮助你