长按时如何检测selectedSegmentIndex?

时间:2013-12-16 09:33:34

标签: ios uigesturerecognizer uisegmentedcontrol

UILongPressGestureRecognizer已添加到UISegmentedControl。 有没有办法在长按时检测selectedSegmentIndex? 提前谢谢。

2 个答案:

答案 0 :(得分:3)

您是否尝试过添加UILongPressGestureRecognizer?在viewDidLoad中:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPress.delegate = self;
[segmentedControl addGestureRecognizer:longPress];

不要忘记将UIGestureRecognizerDelegate添加到头文件中。

知道在哪里按下:

- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:segmentedControl];
}

然后你可以检查segmentedControl的哪个段与CGPoint p匹配,例如检查Y坐标。当它从UISegmentedControl的中间行离开时,它是段0,当它在该行的右边时它是段1。

答案 1 :(得分:1)

您在UISegmentedControl中注册长按

select
m.match_id,
t1.team_name as home_team_name,
t2.team_name as away_team_name,
p1.player_name as home_team_player_1_name
p2.player_name as home_team_player_2_name
from matches m
left join teams t1 on t1.team_id = m.home_team_id
left join teams t2 on t2.team_id = m.away_team_id
left join players p1 on p1.player_id = m.home_team_player_1_id
left join players p2 on p2.player_id = m.home_team_player_2_id

您可以按如下方式获取所选按钮的索引,并假设分段等间距

  let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ListQuizViewController.segmentLongPress(_:)))
    //longPress.delegate = self;
    self.segmentedControl.addGestureRecognizer(longPress)
    longPress.minimumPressDuration = 1