UISegmentedControl delegate/Touch Events

2019-06-15 00:51发布

问题:

I have a UISegmentedControl that has six segments, I want them to call a method when the value changes, but also when each segment gets a UIControlEventTouchDragIn because I want a UILabel to show up with it's name when the person is trying to select something on it and drags their finger across the control, but the segmented control does not sen this call even when I drag into the thing. Please help. Maybe I need to create UIButtons over the control that call methods upon a drag in, but call the value changed method when they recieve a tap.

回答1:

You could just subclass the UISegmentedControl and override - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { return; } method to manage the dragging and location of the touches.

Being that you wouldn't be able to tell which segment the drag is on anyway, it might be better for you to do everything manually with touch and track it using those functions.



回答2:

Maybe I don't understand your question correctly but couldn't you just use this:

[segmentedControl addTarget:self 
         action:@selector(action:)  
         forControlEvents:UIControlEventValueChanged];

and just replace the UIControlEventValueChanged by UIControlEventTouchDragIn? Then in your action method do whatever needs to be done such as changing the text of a UILabel.

I have a UITAbleView with a bunch of cells and an UISegmentedControll in each cell and it works just fine for me.

The documentation for UISegmentedControl seems to be unavailable online but you can see it in XCode's Organiser and the code above is the first example.