0

I have a segmented control,with 5 segments..I want some event to be fired when a touch down happens on any of the segment and i mean tap and hold,without lifting the finger. And when the user lifts his finger,it should reset

I have tried using touchesBegan and touchesEnded but i don't get the current selectedIndex in touchesBegan,here's my code

-(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;
}

Is there any way to achieve a touch down event effect for a segmentedControl,or any other alternative?

vin
  • 1,258
  • 5
  • 20
  • 33
  • In touchesBegan, the segment is not yet selected, so you can't get it. – kientux Oct 23 '15 at 06:59
  • @kientux Is there any way i can get the selected index before?? I want the event to be fired when its touch down – vin Oct 23 '15 at 07:10
  • I think with UISegmentedControl only, there's nothing can do with touch down event, because segment get selected only when touch up. If you really want to do this, you can place a transparent button on each segment, and when button got touched down, call `setSelectedSegmentIndex:`. Then you have both touch down event and selected index. – kientux Oct 23 '15 at 07:18

3 Answers3

0

You can use touchDown: IBAction selector.

nswamy
  • 1,041
  • 9
  • 16
  • 1
    It doesn't work,i already tried,UISegmentedControl works with only valueChanged selector – vin Oct 23 '15 at 07:08
  • Check this thread - http://stackoverflow.com/questions/1620972/uisegmentedcontrol-register-taps-on-selected-segment – nswamy Oct 23 '15 at 07:09
0

You can try to override:

- (void) setSelectedSegmentIndex:(NSInteger)toValue
Flexoid
  • 50
  • 8
0

Quite frankly, I do not think it is possible to do with UISegmentedControl. You do not get selected segment index until touchesEnded:withEvent: is called.

If you want to get in touchesBegan:withEvent:, my advise would be to write your own custom view that look like UISegmentedControl and gives you call back on touchesBegan:withEvent:.

As suggested by kientux above, adding transparent views or buttons on top of each segment in UISegmentedControl could be another potential solution to your problem but it would be very very tricky!

Good luck!

Abhinav
  • 37,684
  • 43
  • 191
  • 309