//UIScrollView 继承自UIView
UIScrollView *scrollView = [[UIScrollViewalloc]initWithFrame:self.view.bounds];
//iOS7之后滚动试图会自动留白(自动向下调节64),NO禁止可以进行常规的计算
//把系统自动调节滚动试图的功能取消
self.automaticallyAdjustsScrollViewInsets =NO;
scrollView.backgroundColor = [UIColorredColor];
UIImageView *imagV = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 400, 600)];
imagV.image = [UIImageimageNamed:@"0020502.jpg"];
//****contentSize 显示内容的大小 如果想要有滚动的效果,需要让contentSize大于scrollView的size大小
scrollView.contentSize =CGSizeMake(imagV.bounds.size.width, imagV.bounds.size.height);
//设置水平方向的滚动条是否显示 默认是YES
scrollView.showsHorizontalScrollIndicator =NO;
//设置垂直方向的滚动条是否显示 默认是YES
scrollView.showsVerticalScrollIndicator = NO;
//设置滚动回弹效果 默认YES
scrollView.bounces = NO;
//设置翻页效果
scrollView.pagingEnabled = YES;
imagV.userInteractionEnabled = YES;
//设置缩放倍数
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
// scrollView.scrollEnabled = YES;
//可以滚动到顶部(点击状态栏部分) 默认是YES
scrollView.scrollsToTop = YES;
//******设置偏移量 默认显示以scrollView的左上角为原点,设置偏离量之后内容的显示会相对于左上进行偏移
scrollView.contentOffset = CGPointMake(40,60);
//设置滚动试图最开始显示的范围
//让滚动试图的contentSize不大于自身size的时候,也具有回弹效果
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;
scrollView.delegate = self;
#import"ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scrollV;
@property(nonatomic,strong)UIPageControl * pageCon;
@end
@implementationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollV=[[UIScrollViewalloc]initWithFrame:CGRectMake(20,40, 281, 384)];//设置大小
self.scrollV.contentSize=CGSizeMake(10*281,384);//设置显示内容大小
self.scrollV.showsHorizontalScrollIndicator=NO;//是否显示水平条
self.scrollV.showsVerticalScrollIndicator=YES;//是否显示垂直条
self.scrollV.pagingEnabled=YES;//是否翻页
self.scrollV.bounces=NO;//是否带回弹效果
for (int i=0; i<10; i++) {
UIImageView* imageV=[[UIImageViewalloc]initWithFrame:CGRectMake(i*281,0,281, 384)];
NSString* name=[NSStringstringWithFormat:@"海贼%.2d.jpg",i+1];
imageV.image=[UIImageimageNamed:name];
[self.scrollVaddSubview:imageV];
}
self.scrollV.layer.borderColor=[UIColorredColor].CGColor;//设置图片线条颜色
self.scrollV.layer.borderWidth=5;//设置线条的宽度
self.pageCon=[[UIPageControlalloc]initWithFrame:CGRectMake(20,384+40+10,281, 40)];
self.pageCon.backgroundColor=[UIColorredColor];//背影色
self.pageCon.numberOfPages=10;//一共有多少页
self.pageCon.currentPage=2;//一进入显示第几页
self.pageCon.layer.borderColor=[UIColorgreenColor].CGColor;//边框的颜色
self.pageCon.layer.borderWidth=5;//边框的宽度
[self.pageConaddTarget:selfaction:@selector(clickPage:)forControlEvents:UIControlEventTouchUpInside ];
self.scrollV.delegate=self;
[self.viewaddSubview:self.scrollV];
[self.viewaddSubview:self.pageCon];
}
-(void)clickPage:(UIPageControl*)page
{//设置偏移量,带动画效果,当页面控制器滑动时带动图片也滑动
[self.scrollVsetContentOffset:CGPointMake(page.currentPage*281,0) animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{//当视图停止的时候,能过偏移量来计算页数
intnum=scrollView.contentOffset.x/281;
self.pageCon.currentPage=num;//设置pageCon的当前页码
}
uipageControl
//UIPageControl 继承自UIControl
self.pageCon.backgroundColor=[UIColor redColor];//背影色
self.pageCon.numberOfPages=10;//一共有多少页
self.pageCon.currentPage=2;//一进入显示第几页
self.pageCon.layer.borderColor=[UIColorgreenColor].CGColor;//边框的颜色
self.pageCon.layer.borderWidth=5;//边框的宽度
UIPageControl *pageCon = [[UIPageControlalloc]initWithFrame:CGRectMake(0, 100, 320, 40)];
pageCon.backgroundColor = [UIColorgrayColor];
//设置UIPageControl圆心点有几个(页数)
pageCon.numberOfPages = 3;
// pageCon.tintColor = [UIColor greenColor];
//未选中的圆心点颜色
pageCon.pageIndicatorTintColor = [UIColorgreenColor];
//当前的圆心点得颜色
pageCon.currentPageIndicatorTintColor =[UIColor blueColor];
//当前页数
pageCon.currentPage = 1;
//注册事件
[pageCon addTarget:selfaction:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:imagV];
[self.view addSubview:scrollView];
//把pageCon添加到view上
[self.view addSubview:pageCon];
self.scrollV.delegate=self;
先设置代理,可以使用以下功能<UIScrollViewDelegate>
-(void)clickPage:(UIPageControl*)page
{//设置偏移量,带动画效果,当页面控制器滑动时带动图片也滑动
[self.scrollV setContentOffset:CGPointMake(page.currentPage*281, 0) animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{//当视图停止的时候,能过偏移量来计算页数
intnum=scrollView.contentOffset.x/281;
self.pageCon.currentPage=num;//设置pageCon的当前页码
}
-(void)goBack:(UIPageControl*)s{
NSLog(@"1111%d",s.currentPage);
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"试图开始滚动");
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"开始拖拽");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{
NSLog(@"试图滚动结束");
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollViewwillDecelerate:(BOOL)decelerate{
NSLog(@"拖拽结束");
}
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"试图滚动到顶部");
//如果需要进行刷新数据等操作
}
//只要滚动,这个方法会议只调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//可以拿到滚动试图实时的偏移量
NSLog(@"试图已经/正在滚动%@",NSStringFromCGPoint(scrollView.contentOffset));
}
-(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
// if (scrollView == scrollView) {
// return YES;
// }
//默认是YES
return YES;
}
NSUserDefaults* user=[NSUserDefaults standardUserDefaults]//用来设置引导页,此页只在安装完成后执行一次,UI第八天。