UIPanGestureRecognizer to create Screen similar to Notification Center
In my application i wanted a Screen similar to the Notification Center of the iPhone.
Where user can navigate through 3 screens using swipe gesture and also by selecting the UISegmentedControl index.
So i have figured out below code.
-(void)viewDidLoad {
[super viewDidLoad];
// I have added a ScrollView on Xib file of width 960px here.
// added content(UIControls) required on first screen at 0px to 320px.
// added content(UIControls) required on second screen at 320px to 640px.
// added content(UIControls) required on third screen at 640px to 960px.
// I have added a UISegmentedControl on Xib file and set an action: - (IBAction)changeTab:(id)sender
// Add PangestureRecognizer to view
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeThroughTabs:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.view addGestureRecognizer:panRecognizer];
}
-(void)swipeThroughTabs:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint velocity = [(UIPanGestureRecognizer*)sender velocityInView:[sender view]];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
if (velocity.x < 0) {
if (segmentedControlForTab.selectedSegmentIndex == 0) {
[self changeTabManually:1];
} else if (segmentedControlForTab.selectedSegmentIndex == 1) {
[self changeTabManually:2];
}
} else {
if (segmentedControlForTab.selectedSegmentIndex == 2) {
[self changeTabManually:1];
} else if (segmentedControlForTab.selectedSegmentIndex == 1) {
[self changeTabManually:0];
}
}
}
}
-(void)changeTabManually:(NSInteger )selectedIndex {
[self hideKeyboard];
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^ {
[segmentedControlForTab setSelectedSegmentIndex:selectedIndex];
CGRect frameOfScrollView = scrollViewSettingPage.frame;
switch (selectedIndex) {
case 0: {
frameOfScrollView.origin.x = 0;
scrollViewSettingPage.frame = frameOfScrollView;
}
break;
case 1: {
frameOfScrollView.origin.x = -320;
scrollViewSettingPage.frame = frameOfScrollView;
}
break;
case 2: {
frameOfScrollView.origin.x = -640;
scrollViewSettingPage.frame = frameOfScrollView;
}
break;
default:
break;
}
}
completion:^(BOOL finished) {
}];
}
- (IBAction)changeTab:(id)sender {
[self hideKeyboard];
UISegmentedControl *segmentedControl = (id)sender;
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^ {
CGRect frameOfScrollView = scrollViewSettingPage.frame;
switch (segmentedControl.selectedSegmentIndex) {
case 0: {
frameOfScrollView.origin.x = 0;
scrollViewSettingPage.frame = frameOfScrollView;
}
break;
case 1: {
frameOfScrollView.origin.x = -320;
scrollViewSettingPage.frame = frameOfScrollView;
}
break;
case 2: {
frameOfScrollView.origin.x = -640;
scrollViewSettingPage.frame = frameOfScrollView;
}
break;
default:
break;
}
}
completion:^(BOOL finished) {
}];
}
Comments
Post a Comment