scrollview滚动计算和UIpagerControl可以做出类似于android的引导页效果,
scrollview可以水平滚动,也可以垂直滚动,很方便
记得实现协议:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIScrollViewDelegate>
@end
主要的代码和注释如下:
#import "ViewController.h"
@interface ViewController ()
@property (strong,nonatomic) UIView *page1;
@property (strong,nonatomic) UIView *page2;
@property (strong,nonatomic) UIView *page3;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat scrollViewHeight = self.scrollerView.frame.size.height;
CGFloat scrollViewWidth = self.view.frame.size.width;
//滚动的方向,滚动的距离总的是三个父控件的宽度和一个自己的高度,即为只有横向的滚动,纵向不滚动
self.scrollerView.contentSize = CGSizeMake(scrollViewWidth * 3,
scrollViewHeight);
/*self.view.frame.size.height- self.pageControl.frame.size.height - self.tabBarController.view.frame.size.height*/
self.scrollerView.frame = self.view.frame;
UIStoryboard *mainBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *page1Controller = [mainBoard instantiateViewControllerWithIdentifier:@"page1"];
self.page1 = page1Controller.view;
self.page1.frame = CGRectMake(0.0f, 0.0f, scrollViewWidth, scrollViewHeight);
UIViewController *page2Controller = [mainBoard instantiateViewControllerWithIdentifier:@"page2"];
self.page2 = page2Controller.view;
self.page2.frame = CGRectMake(scrollViewWidth, 0.0f, scrollViewWidth, scrollViewHeight);
UIViewController *page3Controller = [mainBoard instantiateViewControllerWithIdentifier:@"page3"];
self.page3 = page3Controller.view;
self.page3.frame = CGRectMake(2 * scrollViewWidth, 0.0f, scrollViewWidth, scrollViewHeight);
self.scrollerView.delegate = self;
//添加子view
[self.scrollerView addSubview:self.page1];
[self.scrollerView addSubview:self.page2];
[self.scrollerView addSubview:self.page3];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)changePage:(id)sender {
[UIView animateWithDuration:0.3f animations:^{
NSInteger whichPage = self.pageControl.currentPage;
self.scrollerView.contentOffset = CGPointMake(self.view.frame.size.width*whichPage, 0.0f);
}];
}
//scrollview滚动事件
-(void)scrollViewDidScroll:(UIScrollView *)aScrollView{
CGPoint offset = aScrollView.contentOffset;
self.pageControl.currentPage = offset.x/320.0f;
}
@end