Ios 对UIViewController的某些部分启用用户交互
我从过去3到4个小时一直在研究这个问题,但我没有得到任何信息。我的问题是我想对UIViewController的某些部分启用用户交互 说明: 我有一个UIViewController。我添加了30个表视图。我在应用程序中存储了一个值。如果该值为1,则我必须仅为tableview1启用用户交互;如果该值为2,则仅为tableview2……。等等Ios 对UIViewController的某些部分启用用户交互,ios,objective-c,swift,Ios,Objective C,Swift,我从过去3到4个小时一直在研究这个问题,但我没有得到任何信息。我的问题是我想对UIViewController的某些部分启用用户交互 说明: 我有一个UIViewController。我添加了30个表视图。我在应用程序中存储了一个值。如果该值为1,则我必须仅为tableview1启用用户交互;如果该值为2,则仅为tableview2……。等等 。如果我不清楚,请告诉我。谢谢你花了宝贵的时间。提前感谢一种简单的方法是将UIView子类化并覆盖-(BOOL)pointInside:(CGPoint)
。如果我不清楚,请告诉我。谢谢你花了宝贵的时间。提前感谢一种简单的方法是将UIView子类化并覆盖
-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event
对于希望子视图忽略接触的UIView部分(在示例中表示为ignoreRect
),返回NO
@interface InteractionView ()
@property (nonatomic) CGRect ignoreRect;
@end
@implementation InteractionView
- (void)awakeFromNib {
self.ignoreRect = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (CGRectContainsPoint(self.ignoreRect, point)) {
return NO;
}
return [super pointInside:point withEvent:event];
}
@end
如果需要对预期行为进行更多调整:例如,返回特定区域的特定视图,返回特定区域的俯视图。。。你可以用
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (CGRectContainsPoint(self.ignoreRect, point)) {
return nil; // Edit that part if you want to return a chosen view
}
return [super hitTest:point withEvent:event];
}
一种简单的方法是将UIView子类化并重写
-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event
对于希望子视图忽略接触的UIView部分(在示例中表示为ignoreRect
),返回NO
@interface InteractionView ()
@property (nonatomic) CGRect ignoreRect;
@end
@implementation InteractionView
- (void)awakeFromNib {
self.ignoreRect = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (CGRectContainsPoint(self.ignoreRect, point)) {
return NO;
}
return [super pointInside:point withEvent:event];
}
@end
如果需要对预期行为进行更多调整:例如,返回特定区域的特定视图,返回特定区域的俯视图。。。你可以用
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (CGRectContainsPoint(self.ignoreRect, point)) {
return nil; // Edit that part if you want to return a chosen view
}
return [super hitTest:point withEvent:event];
}
另一个没有
-(UIView*)hitTest:(CGPoint)pointwithevent:(UIEvent*)event的解决方案是将UIButton
作为子视图添加到UIView
要关闭交互的部分。
例如,如果要关闭视图下半部分的交互
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, self.view.frame.size.height*0.5f, self.view.frame.size.width, self.view.frame.size.height*0.5);
[self.view addSubview:button];
由于它将获得触摸事件,因此一半的视图将关闭以供用户交互
编辑
IBOutletCollection(UITableView) NSArray *allTableViews;// get all your tableviews reference to this array. set tag in interface builder for each array to reach later.
然后,当您想要启用/禁用相关tableview的交互时
int tagToOpenInteraction = 1;//or whatever it is
for(UITableView *t in allTableViews)
{
if(t.tag == tagToOpenInteraction)
[t setUserInteractionEnabled:YES];
else
[t setUserInteractionEnabled:NO];
}
另一个没有-(UIView*)hitTest:(CGPoint)pointwithevent:(UIEvent*)event的解决方案是将UIButton
作为子视图添加到UIView
要关闭交互的部分。
例如,如果要关闭视图下半部分的交互
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, self.view.frame.size.height*0.5f, self.view.frame.size.width, self.view.frame.size.height*0.5);
[self.view addSubview:button];
由于它将获得触摸事件,因此一半的视图将关闭以供用户交互
编辑
IBOutletCollection(UITableView) NSArray *allTableViews;// get all your tableviews reference to this array. set tag in interface builder for each array to reach later.
然后,当您想要启用/禁用相关tableview的交互时
int tagToOpenInteraction = 1;//or whatever it is
for(UITableView *t in allTableViews)
{
if(t.tag == tagToOpenInteraction)
[t setUserInteractionEnabled:YES];
else
[t setUserInteractionEnabled:NO];
}
谢谢你。我会尝试一下,并把它作为正确的答案。谢谢。我将尝试并使其成为正确答案难道你不能在30个表视图上循环,设置tableView.userInteractionEnabled=NO,但对于具有良好值的表视图除外?抱歉,我需要帮助实现循环难道你不能在30个表视图上循环,设置tableView.userInteractionEnabled=NO,但对于具有良好值的对象除外?抱歉,我需要帮助实现循环