【问题标题】:UIControll in UIScrollView not receiving touch eventsUIScrollView 中的 UIControll 未接收到触摸事件
【发布时间】:2016-07-20 02:21:53
【问题描述】:

我在我的项目中使用SevenSwitch。我需要将它添加到UIScrollView,但是当我将它添加到滚动视图时,控件似乎无法接收触摸事件。

我尝试对scrollview 进行子类化并覆盖以下代码:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
  return NO;
}

还添加了:

self.scrollView.delaysContentTouches = NO;

但仍然无法接收到触摸事件。如何阻止scrollview 阻止UIControl 接收触摸?

更新

我的滚动视图上有一个点击手势,因为我希望当用户点击滚动视图时我调用[self.scrollView endEditing:YES] 来关闭键盘。当我删除它时,七开关正在与水龙头一起使用。

我将以下代码添加到我的点击手势中:

tap.cancelsTouchesInView = NO;

现在sevenswitch 正在使用点击,但是在使用触摸跟踪切换onoff 时出现问题。

【问题讨论】:

  • 有人知道吗?
  • 为什么要在里面设置触摸事件?我已经通过将 SevenSwitch 放在滚动视图中对其进行了示例演示,并且它可以正常工作(获得控制动作)而无需进行任何更改
  • 我不想有触摸事件。我只希望它有效。当我将它添加到滚动视图时,我无法打开它

标签: ios objective-c uiscrollview uiswitch uicontrol


【解决方案1】:

我制作了一个示例,其中我在滚动视图中有sevenswitch,它工作正常

- (void)viewDidLoad {
    [super viewDidLoad];
    SevenSwitch *mySwitch = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5);
    [mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    //[self.view addSubview:mySwitch];

    mySwitch.on = true;

    [_cntView addSubview:mySwitch];

    SevenSwitch *mySwitch3 = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70);
    [mySwitch3 addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mySwitch3];

    //self.view.backgroundColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
    mySwitch3.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
    mySwitch3.activeColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.inactiveColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.onTintColor = [UIColor colorWithRed:0.45f green:0.58f blue:0.67f alpha:1.00f];
    mySwitch3.borderColor = [UIColor clearColor];
    mySwitch3.shadowColor = [UIColor blackColor];

    [_cntView addSubview:mySwitch3];
}

- (void)switchChanged:(SevenSwitch *)sender {
    NSLog(@"Changed value to: %@", sender.on ? @"ON" : @"OFF");
}

其中_cntView 是我放置在滚动视图中的主容器视图,请检查这是否适合您


更新

正如我在评论中提到的,我没有得到你想要通过触摸跟踪说的内容,但我已经在滚动视图中使用点击手势制作了示例,这可能会有所帮助

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    [scrollview setContentSize:CGSizeMake(self.view.frame.size.width,700)];
    [self.view addSubview:scrollview];

    SevenSwitch *mySwitch = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5);
    [mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    //[self.view addSubview:mySwitch];

    mySwitch.on = true;

    [scrollview addSubview:mySwitch];

    SevenSwitch *mySwitch3 = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70);
    [mySwitch3 addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    mySwitch3.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
    mySwitch3.activeColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.inactiveColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.onTintColor = [UIColor colorWithRed:0.45f green:0.58f blue:0.67f alpha:1.00f];
    mySwitch3.borderColor = [UIColor clearColor];
    mySwitch3.shadowColor = [UIColor blackColor];

    [scrollview addSubview:mySwitch3];


    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSingleTap:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.cancelsTouchesInView = NO;
    [scrollview addGestureRecognizer:singleTap];
}


- (void)actionSingleTap:(UITapGestureRecognizer *)sender {
    NSLog(@"Tap");
}

- (void)switchChanged:(SevenSwitch *)sender {
    NSLog(@"Changed value to: %@", sender.on ? @"ON" : @"OFF");
}

我已经以编程方式编写了所有新代码,它检测到 SevenSwitch 之外的触摸事件,并且还检测到七个开关上的触摸/点击。
如果你想让它在 Storyboard 中制作滚动视图并更改程序化滚动视图故事板出口

【讨论】:

  • 我还在故事板中创建的全新创建视图控制器中创建您所做的事情。现在,sevenswitch 可以与 tap 一起使用,但在轨道触摸方面存在问题。我还以编程方式创建我的滚动视图并将其添加到视图中。在此滚动视图中,即使点击也无法正常工作。
  • @HuseinBehboodiRad 那么现在它是否以编程方式在您的滚动视图中工作?还`touch tacking`意味着它不适用于开关上的平移/拖动手势?
  • 触摸跟踪不适用于情节提要和我以编程方式添加的滚动视图
  • 抱歉,我没有收到您的触摸跟踪问题
  • @HuseinBehboodiRad 我已经更新了代码,如果你还有问题
【解决方案2】:

尝试关注可能对您有所帮助

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];    

你会接触到:

-(void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
  { 
    CGPoint touchPoint=[gesture locationInView:scrollView];
  }

注意:

滚动视图也有一个手势识别器。默认情况下,任何时候只有 1 个手势识别器可以处理触摸。您需要让自己成为手势的代表,然后实现gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 以返回YES。这将允许它与滚动视图同时工作。

【讨论】:

  • 单击即可工作。不需要新的手势。持有和跟踪sevenswitch 是问题所在。它的动画很糟糕。
【解决方案3】:
- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSingleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [self.scrollView addGestureRecognizer:singleTap];
}

- (void)actionSingleTap:(UITapGestureRecognizer *)sender {

    CGPoint touchPoint = [sender locationInView:self.scrollView];
    NSLog(@"Touch point coordinates are : %f - %f", touchPoint.x , touchPoint.y );

    UIView *hitImageView = [self.scrollView hitTest:touchPoint withEvent:nil];
    NSLog(@"%@", hitImageView);

    switch (hitImageView.tag) {
        case 1:
            // do something
            break;

        default:
            break;
    }

}

【讨论】:

  • 单击即可工作。不需要新的手势。持有和跟踪sevenswitch 是问题所在。它有糟糕的动画
猜你喜欢
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多