Skip to content

Instantly share code, notes, and snippets.

@ttsubono
Created June 22, 2012 08:24
Show Gist options
  • Save ttsubono/2971304 to your computer and use it in GitHub Desktop.
Save ttsubono/2971304 to your computer and use it in GitHub Desktop.
NavigationBarに置いたUISegmentedControlのインスタンスを水平回転
- (void)addNavigationBarButton {
// 黒色のNavigationBarに黒色のボタンの場合
// setmentedControlObjectがメンバー変数としてあること
NSArray *controlItems = [NSArray arrayWithObjects:@"BBB", nil];
segmentedControlObject = [[UISegmentedControl alloc] initWithItems:controlItems];
segmentedControlObject.momentary = YES;
segmentedControlObject.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControlObject.tintColor = [UIColor darkGrayColor];
[segmentedControlObject addTarget:self action:@selector(flipNavigationBarButton) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:segmentedControlObject];
self.navigationItem.rightBarButtonItem = button;
[button release];
}
}
- (void)flipNavigationBarButton {
#define FLIP_ANIMATION_DURATION 0.5
static BOOL flipFromLeft = YES;
[UIView animateWithDuration:FLIP_ANIMATION_DURATION
animations:^ {
if (flipFromLeft) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:segmentedControlObject
cache:YES];
[segmentedControlObject setTitle:@"AAA"
forSegmentAtIndex:0];
} else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:segmentedControlObject
cache:YES];
[segmentedControlObject setTitle:@"BBB"
forSegmentAtIndex:0];
}
}
];
flipFromLeft = !flipFromLeft;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment