就不封装了。
.h:
#import <UIKit/UIKit.h>
@interface Guide :UIViewController<UIScrollViewDelegate>
@property (strong,nonatomic) IBOutletUIScrollView *pageScroll;
@property (weak, nonatomic) IBOutletUIPageControl *pageControl;
@property(strong,nonatomic)NSArray *imageArray; //存储图片数组
@end
.m文件:
#define kHeight self.view.frame.size.height //定义图片高度
#define kWidth 320 //定义宽度为320
- (void)viewDidLoad
{
[superviewDidLoad];
[selfsetImageData]; //初始化图片数据
[selfsetimageViewInScrollView];
pageControl.numberOfPages=[imageArraycount];
//设置pageControl的数量为imageArray图片数组的数量
}
-(void)setImageData
{
imageArray=@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];
//初始化数据数组
}
-(void)setimageViewInScrollView
{
pageScroll.directionalLockEnabled=YES;
pageScroll.pagingEnabled=NO;
pageScroll.backgroundColor=[UIColorwhiteColor];
pageScroll.delegate=self;
pageScroll.bounces=NO;
pageScroll.scrollEnabled=YES;
pageScroll.pagingEnabled=YES;
for (int n=0; n<[imageArraycount]; n++)
{
UIScrollView *s = [[UIScrollViewalloc] initWithFrame:CGRectMake(320*n,0, kWidth,kHeight)];
s.backgroundColor = [UIColorclearColor];
s.contentSize =CGSizeMake(320,kHeight);
s.showsHorizontalScrollIndicator =NO;
s.showsVerticalScrollIndicator =NO;
s.delegate = self;
s.tag = n+1;
[s setZoomScale:1.0];
UIImageView *imageview = [[UIImageViewalloc] init];
// imageview.image = [UIImage imageNamed:[array objectAtIndex:i]];
[imageview setImage:[UIImageimageNamed:[imageArrayobjectAtIndex:n]]];
imageview.frame = CGRectMake(0, 0, 320,kHeight);
[imageview setContentMode:UIViewContentModeScaleAspectFill];
imageview.userInteractionEnabled = YES;
imageview.tag = n+1;
[s addSubview:imageview];
[pageScroll addSubview:s];
}
[pageScrollsetContentSize:CGSizeMake
([imageArray count]*kWidth, kHeight)];
}
#pragma mark -scrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)OscrollView{
//当移动时随时监控,这句是PageControl的核心
int index =fabs(pageScroll.contentOffset.x) /pageScroll.frame.size.width;
pageControl.currentPage = index;
if (pageControl.currentPage==3)
{
NSLog(@"最后一页");
}
}