有什么办法可以为UISegmentedControl注册一个触摸屏

时间:2015-10-23 06:35:32

标签: ios uisegmentedcontrol uitouch

我有一个分段控件,有5个段......我希望在任何一个段上发生触摸时触发一些事件,我的意思是点击并按住,而不抬起手指。 当用户抬起手指时,它应该重置

我尝试过使用touchesBegan和touchesEnded但是我没有在touchesBegan中获得当前的selectedIndex,这是我的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    long oldValue = self.selectedSegmentIndex;
    [super touchesBegan:touches withEvent:event];
    if (oldValue == self.selectedSegmentIndex )
        [self sendActionsForControlEvents:UIControlEventValueChanged];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
    NSLog(@"selectedIndex%lu",self.selectedSegmentIndex);
    [super touchesEnded:touches withEvent:event];
    self.selectedSegmentIndex = -1;
}

有没有办法为segmentedControl或任何其他替代方案实现降落事件效果?

3 个答案:

答案 0 :(得分:0)

您可以使用touchDown:IBAction选择器。

答案 1 :(得分:0)

您可以尝试覆盖:

- (void) setSelectedSegmentIndex:(NSInteger)toValue

答案 2 :(得分:0)

坦率地说,我认为8 cbrt 无法做到这一点。在调用UISegmentedControl之前,您不会获得选定的段索引。

如果您想进入touchesEnded:withEvent:,我的建议是编写您自己的自定义视图,该视图看起来像touchesBegan:withEvent:,然后回复UISegmentedControl

正如上面的kientux所建议的那样,在touchesBegan:withEvent:中的每个片段顶部添加透明视图或按钮可能是您问题的另一个潜在解决方案,但它会非常棘手!

祝你好运!

相关问题