UIScrollView+PageControl 翻页提示
2013-12-17 09:57
288 查看
就不封装了。
.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(@"最后一页");
}
}
.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(@"最后一页");
}
}
相关文章推荐
- 用uiscrollView和pageControl 实现翻页
- UIScrollView结合UIPageControl+NSTimer使用简介
- 学习IOS开发UI篇--UIScrollView/delegate/pagecontrol/UITimer
- 实习笔记:uiscrollview与pagecontrol的综合应用
- ios菜鸟之路:uiscrollview和pagecontrol的使用方法
- ios例子12 pageControl和UIScrollView
- 顶部可滚动小广告位 的封装 (UIScrollView+PageControl)
- 图片轮播器(UIScrollView-UIPageControl)
- UIScrollView和自定义PageControl的结合使用
- [IOS UIScrollView+PageControl]信息展示横幅
- UIScrollView--UIPageControl
- 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例
- (九)UIScrollView和PageControl的分页
- (九)UIScrollView和PageControl的分页
- UISCrollView 与 UIPageControl 之间产生联系,就靠这句代码pageControl0.currentPage = page;
- 相册翻页效果ScrollView+pageControl
- ios PageControl and UIScrollView
- swift中UIScrollView的使用(结合pageControl,timer计时的使用)
- ios菜鸟之路:uiscrollview和pagecontrol的使用方法
- iOS中UIScrollView和UIPageControl以及NSTimer的综合使用