本代码主要实现在固定的位置滑动图片可以切换。
目录图如下:
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
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(, , WIDTH, HEIGHT)];
self.myScrollView.backgroundColor=[UIColor colorWithRed:0.315 green:0.843 blue:0.892 alpha:1.000];
self.myScrollView.contentSize=CGSizeMake(*WIDTH, HEIGHT);
// 设置分页
self.myScrollView.pagingEnabled=YES;
// 隐藏滚动条
self.myScrollView.showsHorizontalScrollIndicator=NO;
// 锁定滚动方向
self.myScrollView.directionalLockEnabled=YES;
// 引用代理
self.myScrollView.delegate=self; [self.view addSubview:self.myScrollView]; self.myPageControl=[[UIPageControl alloc] init];
CGSize PageSize=CGSizeMake(, );
self.myPageControl.frame=CGRectMake((WIDTH-PageSize.width)/, HEIGHT-PageSize.height-, PageSize.width, PageSize.height);
self.myPageControl.backgroundColor=[UIColor clearColor]; // 设置当前页
self.myPageControl.currentPage=;
// 分页
self.myPageControl.numberOfPages=; [self.view addSubview:self.myPageControl]; // 初始化储存图片的集合
self.imageArray=[NSMutableArray arrayWithCapacity:];
for (int i=; i<; i++) {
UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[self.imageArray addObject:image];
} self.firstimage =[[UIImageView alloc] init];
self.secondimage=[[UIImageView alloc] init];
self.thirimage =[[UIImageView alloc] init];
// 当前页码
self.currentPage=; [self reloadImage]; } -(void)reloadImage
{
// 第一种情况, 第一页
if (self.currentPage==) {
self.firstimage.image=[self.imageArray lastObject];
self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+]; } // 第二种情况,最后一页
else if (self.currentPage==self.imageArray.count-){
self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-];
self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
self.thirimage.image=[self.imageArray objectAtIndex:]; } // 第三种情况 中间页
else{
self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-];
self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+];
} self.firstimage.frame=CGRectMake(, , WIDTH, HEIGHT);
self.secondimage.frame=CGRectMake(WIDTH, , WIDTH, HEIGHT);
self.thirimage.frame=CGRectMake(WIDTH*, , WIDTH, HEIGHT); [self.myScrollView addSubview:self.firstimage];
[self.myScrollView addSubview:self.secondimage];
[self.myScrollView addSubview:self.thirimage]; self.myScrollView.contentOffset=CGPointMake(WIDTH, );
} #pragma mark scrollView Delegate
//在滚动结束状态转换
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float x=self.myScrollView.contentOffset.x; // 向左
if (x<||x==) {
if (self.currentPage==) {
self.currentPage=(int)self.imageArray.count-;
}else{
self.currentPage--;
}
} // 向右
if (x>*WIDTH||x==*WIDTH) {
if (self.currentPage==(int)self.imageArray.count-) {
self.currentPage=;
}else{
self.currentPage++;
}
} self.myPageControl.currentPage=self.currentPage;
[self reloadImage];
}