Is it possible to deselect a segment of the UISegmentedControl
by tapping it the second time? I am currently using a UILongPressGestureRecognizer
object to setSelectedSegmentIndex
to UISegmentedControlNoSegment
. However, I'd rather have the selected segment deselect on the second tap.
Another method is to subclass the UISegmentedControl
. For example:
@interface ToggleSegmentedControl : UISegmentedControl
{
NSUInteger selectedSegment;
}
@end
@implementation ToggleSegmentedControl
-(id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
selectedSegment = self.selectedSegmentIndex;
}
return self;
}
-(void)awakeFromNib
{
selectedSegment = self.selectedSegmentIndex;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if( selectedSegment == self.selectedSegmentIndex && selectedSegment!=-1)
{
self.selectedSegmentIndex= UISegmentedControlNoSegment;
selectedSegment=-1;
}
else
{
selectedSegment=self.selectedSegmentIndex;
}
}
@end
The above code may require further tweaking to suit individual specifications.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.