Skip to content

Instantly share code, notes, and snippets.

@alekseypotapov-dev
Created February 27, 2014 09:05
Show Gist options
  • Save alekseypotapov-dev/9246736 to your computer and use it in GitHub Desktop.
Save alekseypotapov-dev/9246736 to your computer and use it in GitHub Desktop.
Make possibility to react segment on continious tap and stay highlighted in custom UISegmentedControl
+ (BOOL)isIOS7
{
static BOOL isIOS7 = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSInteger deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
if (deviceSystemMajorVersion >= 7) {
isIOS7 = YES;
}
else {
isIOS7 = NO;
}
});
return isIOS7;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
[super touchesBegan:touches withEvent:event];
if (![[self class] isIOS7])
{
// before iOS7 the segment is selected in touchesBegan
if (previousSelectedSegmentIndex == self.selectedSegmentIndex)
{
// if the selectedSegmentIndex before the selection process is equal to the selectedSegmentIndex
// after the selection process the superclass won't send a UIControlEventValueChanged event.
// So we have to do this ourselves.
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
[super touchesEnded:touches withEvent:event];
if ([[self class] isIOS7])
{
// on iOS7 the segment is selected in touchesEnded
if (previousSelectedSegmentIndex == self.selectedSegmentIndex)
{
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment