经过网上一个demo的分析和运用加以改善的万能代码,只需将下面的.h和.m文件的代码直接复制到你的工程的这个控制器的。h和。m文件下即可,不过还是要看一下代码中property声明的东西,这里面- (void)createPages方法里的图片名字换成你的图片名字就好了,如有疑问联系我哦
不多说,直接上码:
.h文件
#import <UIKit/UIKit.h>
@interface PageViewController : UIViewController<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
- (void)createPages;
- (IBAction)return:(id)sender;
@end
.m文件
#import “PageViewController.h”
@interface PageViewController ()
- (void)loadScrollViewWithPage:(UIView *)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end
@implementation PageViewController
@synthesize scrollView;
@synthesize pageControl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"mysteel_login_bg"]]];
int pageCount = 5;
CGRect scrollViewRect = [self.view bounds];
//create scrollview
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollViewRect.size.width * (pageCount+1),0);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.delegate = self;
pageControl.numberOfPages = pageCount;
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
//create pages
[self createPages];
// Do any additional setup after loading the view.
[self.view addSubview:pageControl];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[self setScrollView:nil];
[self setPageControl:nil];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark 图片切换
- (void)loadScrollViewWithPage:(UIView *)page
{
int pageCount = [[scrollView subviews] count]; //这一句施舍呢意思?
CGRect bounds = scrollView.bounds;
bounds.origin.x = bounds.size.width * pageCount;
bounds.origin.y = 0;
page.frame = bounds;
[scrollView addSubview:page];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = sender.frame.size.width;
int page = floor((sender.contentOffset.x – pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)createPages
{
CGRect pageRect = scrollView.frame;
//create pages
UIView *page1 = [[UIView alloc] initWithFrame:pageRect];
page1.backgroundColor = [UIColor blackColor];
[page1 setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @"1.png"]]];
UIView *page2 = [[UIView alloc] initWithFrame:pageRect];
page2.backgroundColor = [UIColor blackColor];
[page2 setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @"2.png"]]];
UIView *page3 = [[UIView alloc] initWithFrame:pageRect];
page3.backgroundColor = [UIColor blackColor];
[page3 setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @"3.png"]]];
UIView *page4 = [[UIView alloc] initWithFrame:pageRect];
page4.backgroundColor = [UIColor blackColor];
[page4 setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @"4.png"]]];
UIView *page5 = [[UIView alloc] initWithFrame:pageRect];
page5.backgroundColor = [UIColor blackColor];
[page5 setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @"5.png"]]];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 5, self.scrollView.frame.size.height);
[self loadScrollViewWithPage:page1];
[self loadScrollViewWithPage:page2];
[self loadScrollViewWithPage:page3];
[self loadScrollViewWithPage:page4];
[self loadScrollViewWithPage:page5];
}
- (void)changePage:(id)sender
{
int page = pageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
}
- (IBAction)return:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end