Akoya Akoya - 4 months ago 29
iOS Question

How can I detect the selectedSegmentIndex when long-pressing down?

UILongPressGestureRecognizer got added to UISegmentedControl.
Is there a way to detect selectedSegmentIndex when long-pressing down?
thanks, in advance.

Answer

Did you try adding a UILongPressGestureRecognizer to it? In viewDidLoad:

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

Don't forget to add UIGestureRecognizerDelegate to your header file.

To know where is pressed:

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

Then you could check what segment of segmentedControl matches with CGPoint p, check for the Y-coordinate, for example. When it's left from the middle line of the UISegmentedControl it's segment 0, when it's right of that line it's segment 1.