Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an method to handle select common segment #338

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion HMSegmentedControl/HMSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@class HMSegmentedControl;

typedef void (^IndexChangeBlock)(NSInteger index);
typedef void (^IndexChangeBlock)(NSUInteger index, BOOL isChange);
typedef NSAttributedString *(^HMTitleFormatterBlock)(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected);

typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionStyle) {
Expand Down
7 changes: 6 additions & 1 deletion HMSegmentedControl/HMSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,11 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// Check if we have to do anything with the touch event
if (self.isTouchEnabled)
[self setSelectedSegmentIndex:segment animated:self.shouldAnimateUserSelection notify:YES];
}else {
//Handle the index not changed
if (self.indexChangeBlock) {
self.indexChangeBlock(segment, NO);
}
}
}
}
Expand Down Expand Up @@ -939,7 +944,7 @@ - (void)notifyForSegmentChangeToIndex:(NSInteger)index {
[self sendActionsForControlEvents:UIControlEventValueChanged];

if (self.indexChangeBlock)
self.indexChangeBlock(index);
self.indexChangeBlock(index, YES);
}

#pragma mark - Styling Support
Expand Down
6 changes: 3 additions & 3 deletions HMSegmentedControlExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ - (void)viewDidLoad {
// Segmented control with more customization and indexChangeBlock
HMSegmentedControl *segmentedControl3 = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"One", @"Two", @"Three", @"4", @"Five"]];
[segmentedControl3 setFrame:CGRectMake(0, 180, viewWidth, 50)];
[segmentedControl3 setIndexChangeBlock:^(NSInteger index) {
NSLog(@"Selected index %ld (via block)", (long)index);
[segmentedControl3 setIndexChangeBlock:^(NSUInteger index, BOOL isChange) {
NSLog(@"Selected index %ld,is changed %d", (long)index,isChange);
}];
segmentedControl3.selectionIndicatorHeight = 4.0f;
segmentedControl3.backgroundColor = [UIColor colorWithRed:0.1 green:0.4 blue:0.8 alpha:1];
Expand Down Expand Up @@ -109,7 +109,7 @@ - (void)viewDidLoad {
self.segmentedControl4.tag = 3;

__weak typeof(self) weakSelf = self;
[self.segmentedControl4 setIndexChangeBlock:^(NSInteger index) {
[self.segmentedControl4 setIndexChangeBlock:^(NSUInteger index, BOOL isChange) {
[weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 200) animated:YES];
}];

Expand Down