uiscrollview和uipagecontrol组合

经过网上一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值