IOS平鋪導航
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property(strong,nonatomic)UIView *page1;
@property(strong,nonatomic)UIView *page2;
@property(strong,nonatomic)UIView *page3;
@property(strong,nonatomic)UIView *page4;
- (IBAction)changePage:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*4, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;
UIStoryboard *mainStoryBoard = self.storyboard;
UIViewController*page1ViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"page1"];
self.page1 = page1ViewController.view;
self.page1.frame = CGRectMake(0,0,self.view.frame.size.width,420);
UIViewController*page2ViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"page2"];
self.page2 = page2ViewController.view;
self.page2.frame = CGRectMake(320, 0,320, 420);
UIViewController* page3ViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"page3"];
self.page3 = page3ViewController.view;
self.page3.frame = CGRectMake(320*2, 0, 320, 420);
UIViewController* page4ViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"page4"];
self.page4 = page4ViewController.view;
self.page4.frame =CGRectMake(320*3, 0,320, 420);
self.scrollView.delegate = self;
[self.scrollView addSubview:self.page1];
[self.scrollView addSubview:self.page2];
[self.scrollView addSubview:self.page3];
[self.scrollView addSubview:self.page4];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)scrollViewDidScroll:(UIScrollView *)ascrollView
{
CGPoint offset =ascrollView.contentOffset;
self.pageControl.currentPage = offset.x/320;
}
- (IBAction)changePage:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
NSInteger whichPage = self.pageControl.currentPage;
self.scrollView.contentOffset = CGPointMake(320*whichPage, 0);
}];
}
@end