码迷,mamicode.com
首页 > 移动开发 > 详细

iOS— UIScrollView和 UIPageControl之间的那些事

时间:2016-03-12 20:00:07      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

本代码主要实现在固定的位置滑动图片可以切换。

目录图如下:

技术分享

 

ViewController.h

#import <UIKit/UIKit.h>
// 通过宏定义定义宽和高
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ViewController : UIViewController<UIScrollViewDelegate>

@property(strong,nonatomic) UIScrollView *myScrollView;
@property(strong,nonatomic) UIPageControl *myPageControl;
// 储存图片的集合
@property(strong,nonatomic)NSMutableArray *imageArray;

// 储存照片
@property(strong,nonatomic)UIImageView *firstimage;
@property(strong,nonatomic) UIImageView *secondimage;
@property(strong,nonatomic) UIImageView *thirimage;
// 当前页码
@property(assign,nonatomic)int currentPage;

@end

ViewController.m

技术分享
  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 
  5 @end
  6 
  7 @implementation ViewController
  8 
  9 - (void)viewDidLoad {
 10     [super viewDidLoad];
 11     self.myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
 12     self.myScrollView.backgroundColor=[UIColor colorWithRed:0.315 green:0.843 blue:0.892 alpha:1.000];
 13     self.myScrollView.contentSize=CGSizeMake(3*WIDTH, HEIGHT);
 14     // 设置分页
 15     self.myScrollView.pagingEnabled=YES;
 16     //  隐藏滚动条
 17     self.myScrollView.showsHorizontalScrollIndicator=NO;
 18     // 锁定滚动方向
 19     self.myScrollView.directionalLockEnabled=YES;
 20     // 引用代理
 21     self.myScrollView.delegate=self;
 22 
 23     [self.view addSubview:self.myScrollView];
 24 
 25     self.myPageControl=[[UIPageControl alloc] init];
 26     CGSize PageSize=CGSizeMake(120, 40);
 27     self.myPageControl.frame=CGRectMake((WIDTH-PageSize.width)/2, HEIGHT-PageSize.height-40, PageSize.width, PageSize.height);
 28     self.myPageControl.backgroundColor=[UIColor clearColor];
 29 
 30     // 设置当前页
 31     self.myPageControl.currentPage=0;
 32     // 分页
 33     self.myPageControl.numberOfPages=4;
 34 
 35     [self.view addSubview:self.myPageControl];
 36 
 37     //  初始化储存图片的集合
 38     self.imageArray=[NSMutableArray arrayWithCapacity:1];
 39     for (int i=1; i<5; i++) {
 40         UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
 41         [self.imageArray addObject:image];
 42     }
 43 
 44     self.firstimage =[[UIImageView alloc] init];
 45     self.secondimage=[[UIImageView alloc] init];
 46     self.thirimage =[[UIImageView alloc] init];
 47     //  当前页码
 48     self.currentPage=0;
 49 
 50     [self reloadImage];
 51 
 52 }
 53 
 54 -(void)reloadImage
 55 {
 56     // 第一种情况, 第一页
 57     if (self.currentPage==0) {
 58         self.firstimage.image=[self.imageArray lastObject];
 59         self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
 60         self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+1];
 61 
 62     }
 63 
 64     // 第二种情况,最后一页
 65     else if (self.currentPage==self.imageArray.count-1){
 66         self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-1];
 67         self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
 68         self.thirimage.image=[self.imageArray objectAtIndex:0];
 69 
 70     }
 71 
 72     // 第三种情况  中间页
 73     else{
 74         self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-1];
 75         self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
 76         self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+1];
 77     }
 78 
 79     self.firstimage.frame=CGRectMake(0, 0, WIDTH, HEIGHT);
 80     self.secondimage.frame=CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
 81     self.thirimage.frame=CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT);
 82 
 83     [self.myScrollView addSubview:self.firstimage];
 84     [self.myScrollView addSubview:self.secondimage];
 85     [self.myScrollView addSubview:self.thirimage];
 86 
 87     self.myScrollView.contentOffset=CGPointMake(WIDTH, 0);
 88 }
 89 
 90 #pragma mark   scrollView  Delegate
 91 //在滚动结束状态转换
 92 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 93 {
 94     float x=self.myScrollView.contentOffset.x;
 95 
 96     // 向左
 97     if (x<0||x==0) {
 98         if (self.currentPage==0) {
 99             self.currentPage=(int)self.imageArray.count-1;
100         }else{
101             self.currentPage--;
102         }
103     }
104 
105     // 向右
106     if (x>2*WIDTH||x==2*WIDTH) {
107         if (self.currentPage==(int)self.imageArray.count-1) {
108             self.currentPage=0;
109         }else{
110             self.currentPage++;
111         }
112     }
113 
114     self.myPageControl.currentPage=self.currentPage;
115     [self reloadImage];
116 }
View Code

 

iOS— UIScrollView和 UIPageControl之间的那些事

标签:

原文地址:http://www.cnblogs.com/bolin-123/p/5269839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!