【问题标题】:Duplicate of actions UIView with UITableViewUIView 与 UITableView 的重复操作
【发布时间】:2015-10-28 16:41:26
【问题描述】:

我在主视图中有表格。接下来,我将视图添加到高度为 100 点的上表视图,然后我的第一行高度为 100 点。接下来我检查 scrollView contentOffset 以重新计算我添加的视图的位置。当我很好地滚动表格视图时,我的视图与表格视图中的第一个单元格重复位置。

但我的问题是: 当我想从我的视图(视图中的手指)滚动并向下滚动时,表格视图与我的手势没有交互。是的,这很正常。如果我将用户交互设置为 NO,一切正常。但是我在这个视图中有一些按钮,当我禁用用户交互时,我不能在这个视图中对我的子视图进行操作。

如何在没有用户交互或其他方法的情况下同时/一起滚动和点击。谢谢。

所有代码:

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
    UIView *someView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];


    someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    someView.backgroundColor = [UIColor redColor];
    [self.view addSubview:someView];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.row == 0) return 100;
    else return 44;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        if (indexPath.row == 0) cell.backgroundColor = [UIColor greenColor];
    }

    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    someView.center = CGPointMake(someView.center.x, -scrollView.contentOffset.y+50);
}

【问题讨论】:

  • 在 tableview 或 uiview 上点击手势会使按钮无法响应早期点击事件

标签: ios objective-c uitableview uiview uiscrollview


【解决方案1】:

尝试在主视图上添加按钮

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];


    someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    someView.backgroundColor = [UIColor redColor];
    [self.view addSubview:someView];

    UIButton *someBotton = [UIButton buttonWithType:UIButtonTypeCustom];
    // button initialisation
    ...
    [self.view addSubview:someBotton];
}

在这种情况下,表格不会只在按钮下方进行交互

第二种方法是为 someView 创建子类并覆盖方法 pointInside:

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event{
   for(UIView *subview in self.subviews){       
       if([subview pointInside:point withEvent:event]){
           return YES;
       }
   }
   return NO;
}

【讨论】:

  • 我需要按钮在滚动时与表格视图具有相同的偏移量,这就是为什么我添加了同时交互表格视图的视图
  • 第二种方法是为 someView 创建子类并覆盖方法 hitTest
  • 第二种方法工作正常,好的。但是 pointInside 阻止了所有 UIGesturesRecognizer 方法,但我使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多