1、UIScorollView 是ios中提供的滑动控件,用来解决当内容区域大于scorollView可视区域时,可以通过滑动的方式查看整个内容区域,UIScorollView 的滑动控件的基类,后期药学的UITableView(表视图),内部实现的原理是修改视图的bounds
//1.创建对象
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20,kScreenWidth - 20 * 2, kScreenHeight - 20 *2)];
//2.配置属性
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20,kScreenWidth - 20 * 2, kScreenHeight - 20 *2)];
//2.配置属性
// scrollView.backgroundColor = [UIColor redColor];
//2.1 设置内容区域的大小
scrollView.contentSize = CGSizeMake(500, 1000);
//2.2 修改内容区域的偏移量
//x变大,往左偏移,y变大往上偏移 1
// scrollView.contentOffset = CGPointMake(140, 400);
//2.3 是否显示滚动指示条
scrollView.showsHorizontalScrollIndicator = NO;//默认为YES
scrollView.showsVerticalScrollIndicator = NO;//默认为YES
//2.4 修改滚动指示条的样式
// scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//2.5 设置scrollView 能否滚动
scrollView.scrollEnabled = YES;//默认为YES
//2.6 设置方向锁,设置滑动时只能从一个方向滚动
scrollView.directionalLockEnabled = NO;//默认为NO
//2.7 设置是否整屏滚动
//2.1 设置内容区域的大小
scrollView.contentSize = CGSizeMake(500, 1000);
//2.2 修改内容区域的偏移量
//x变大,往左偏移,y变大往上偏移 1
// scrollView.contentOffset = CGPointMake(140, 400);
//2.3 是否显示滚动指示条
scrollView.showsHorizontalScrollIndicator = NO;//默认为YES
scrollView.showsVerticalScrollIndicator = NO;//默认为YES
//2.4 修改滚动指示条的样式
// scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//2.5 设置scrollView 能否滚动
scrollView.scrollEnabled = YES;//默认为YES
//2.6 设置方向锁,设置滑动时只能从一个方向滚动
scrollView.directionalLockEnabled = NO;//默认为NO
//2.7 设置是否整屏滚动
scrollView.pagingEnabled = YES;//默认为NO
//2.8 设置当滑动到屏幕边缘的时候是否出现反弹效果
scrollView.bounces = YES;//默认为YES
//2.9 设置当内容区域等于或小于可视区域时,依然具有边界反弹效果
//水平方向
scrollView.alwaysBounceHorizontal = YES;//默认为NO
//垂直方向
scrollView.alwaysBounceVertical = YES;//默认为NO
//2.10 设置点击状态栏,scrollView 是否回到顶部,此时y轴的偏移量是0
//2.10 设置点击状态栏,scrollView 是否回到顶部,此时y轴的偏移量是0
scrollView.scrollsToTop = YES;//默认为YES
//2.11 scrollView 的代理属性
//self指试图控制器对象
//self指试图控制器对象
scrollView.delegate = self;
//2.12 设置scrollView的缩放比例
scrollView.minimumZoomScale = 0.5;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 3.0;
UIImage *image = [UIImage imageNamed: @"4.JPG"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.tag = 101;
imageView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
//添加到父视图
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
//释放所有权
[scrollView release];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.tag = 101;
imageView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
//添加到父视图
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
//释放所有权
[scrollView release];
}
——————————————————————————-——————
scrollView 的代理属性的13个方法及用途
//触发时机:ScrollView 将要开始拖拽idea时候触发(此时手指刚刚触摸到scrollView的时候)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"开始拖拽了没?");
}
//触发时机:ScrollView 将要结束拖拽的时候触发
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffsetNS_AVAILABLE_IOS(5_0){
NSLog(@"拖拽结束了没?");
}
//触发时机:ScrollView 已经结束拖拽的时候触发
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"结束了没?好快啊!");
//在此方法中能拿到contenOfSet的偏移量
}
//触发时机:将要减速的时候触发(此方法不一定触发,匀速不一定触发)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"减速了么?");
}
//触发时机:已经结束减速的时候触发(此方法不一定触发,上面的减速方法不触发,这个结束减速的方法就不会触发)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"加速完了吗?");
//一般在这个方法中获取scrollView的偏移量,或者设置scrollView的偏移量
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"开始拖拽了没?");
}
//触发时机:ScrollView 将要结束拖拽的时候触发
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffsetNS_AVAILABLE_IOS(5_0){
NSLog(@"拖拽结束了没?");
}
//触发时机:ScrollView 已经结束拖拽的时候触发
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"结束了没?好快啊!");
//在此方法中能拿到contenOfSet的偏移量
}
//触发时机:将要减速的时候触发(此方法不一定触发,匀速不一定触发)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"减速了么?");
}
//触发时机:已经结束减速的时候触发(此方法不一定触发,上面的减速方法不触发,这个结束减速的方法就不会触发)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"加速完了吗?");
//一般在这个方法中获取scrollView的偏移量,或者设置scrollView的偏移量
// scrollView.contentOffset = CGPointZero;
// [scrollView setContentOffset:CGPointZero animated:YES];
}
//触发时机:scrollView结束减速并且必须有动画效果才会触发(必须要有动画效果偶)
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
NSLog(@"有动画吗");
}
//触发时机,返回缩放后的视图,但是只能返回scrollView(内容)上的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:101];
}
//触发时机:开始缩放的时候触发
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)viewNS_AVAILABLE_IOS(3_2){
NSLog(@"开始缩放了吗");
}
//触发时机:结束缩放的时候触发
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
NSLog(@"结束缩放了吗");
}
//触发时机:点击状态栏,调用该方法,该方法能实现的前提是scrollToTop的属性是YES
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
return YES;
}
//触发时机:scrollView 已经回到顶部了
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
}
//触发时机:scrollView结束减速并且必须有动画效果才会触发(必须要有动画效果偶)
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
NSLog(@"有动画吗");
}
//触发时机,返回缩放后的视图,但是只能返回scrollView(内容)上的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:101];
}
//触发时机:开始缩放的时候触发
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)viewNS_AVAILABLE_IOS(3_2){
NSLog(@"开始缩放了吗");
}
//触发时机:结束缩放的时候触发
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
NSLog(@"结束缩放了吗");
}
//触发时机:点击状态栏,调用该方法,该方法能实现的前提是scrollToTop的属性是YES
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
return YES;
}
//触发时机:scrollView 已经回到顶部了
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
}
总结:scrollView的使用技巧
使用scrollViewDidZoom: 实时修改自身视图的大小
使用scrollViewDidEndDecelerating: 获取contentOfSet
使用scrollViewDidZoom: 实时修改自身视图的大小
使用scrollViewDidEndDecelerating: 获取contentOfSet
==============================================================
LaunchViewController.m
#import "LaunchViewController.h"
#import "PageViewController.h"
//图片个数
#define kImageCount 6
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface LaunchViewController ()<</span>UIScrollViewDelegate>
//定义属性方便使用
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIPageControl *pageControl;
@end
@implementation LaunchViewController
- (void)dealloc{
_scrollView = nil;
_pageControl = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//再添加scrollView
[self layoutScrollView];
//先添加pageControl
[self layoutPageControl];
}
//布局scrollView
- (void)layoutScrollView{
self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//设置内容页的大小
self.scrollView.contentSize = CGSizeMake(kImageCount * kScreenWidth, kScreenHeight);
//隐藏水平指示条
self.scrollView.showsHorizontalScrollIndicator = NO;
//整屏滑动
self.scrollView.pagingEnabled = YES;
//制定代理
self.scrollView.delegate = self;
//添加图片
for (int i = 0; i <</span> kImageCount; i ++ ) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i *kScreenWidth, 0, kScreenWidth, kScreenHeight)];
//图片名
NSString *name = [NSString stringWithFormat:@"v6_guide_%d",i + 1];
//初始化UIImage 对象
UIImage *image = [UIImage imageNamed:name];
imageView.image = image;
[self.scrollView addSubview:imageView];
[imageView release];
//添加手势
if (kImageCount - 1 == i) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleTap: )];
#import "PageViewController.h"
//图片个数
#define kImageCount 6
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface LaunchViewController ()<</span>UIScrollViewDelegate>
//定义属性方便使用
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIPageControl *pageControl;
@end
@implementation LaunchViewController
- (void)dealloc{
_scrollView = nil;
_pageControl = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//再添加scrollView
[self layoutScrollView];
//先添加pageControl
[self layoutPageControl];
}
//布局scrollView
- (void)layoutScrollView{
self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//设置内容页的大小
self.scrollView.contentSize = CGSizeMake(kImageCount * kScreenWidth, kScreenHeight);
//隐藏水平指示条
self.scrollView.showsHorizontalScrollIndicator = NO;
//整屏滑动
self.scrollView.pagingEnabled = YES;
//制定代理
self.scrollView.delegate = self;
//添加图片
for (int i = 0; i <</span> kImageCount; i ++ ) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i *kScreenWidth, 0, kScreenWidth, kScreenHeight)];
//图片名
NSString *name = [NSString stringWithFormat:@"v6_guide_%d",i + 1];
//初始化UIImage 对象
UIImage *image = [UIImage imageNamed:name];
imageView.image = image;
[self.scrollView addSubview:imageView];
[imageView release];
//添加手势
if (kImageCount - 1 == i) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleTap: )];
[imageView addGestureRecognizer:tap];
[tap release];
//打开用户交互
imageView.userInteractionEnabled = YES;
}
}
self.scrollView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.scrollView];
[self.scrollView release];
}
self.scrollView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.scrollView];
[self.scrollView release];
}
PageViewController.m
#pragma mark pageControl的实现
- (void)layoutPageControl{
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kScreenHeight - 60,kScreenWidth, 60)];
_pageControl.numberOfPages = kImageCount;
//配置点的颜色
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//配置其他点的颜色
- (void)layoutPageControl{
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kScreenHeight - 60,kScreenWidth, 60)];
_pageControl.numberOfPages = kImageCount;
//配置点的颜色
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//配置其他点的颜色
_pageControl.pageIndicatorTintColor = [UIColor greenColor];
//给pageControl 关联事件
[_pageControl addTarget:self action:@selector(handlePage : ) forControlEvents:(UIControlEventValueChanged)];
[self.view addSubview:_pageControl];
[_pageControl release];
}
#pragma mark 是轻拍手势的实现
- (void)handleTap : (UITapGestureRecognizer *)tap{
NSLog(@"轻拍了嘛?");
//是一个单例,可以实现数据持久化,当用户点击到最后一张图片时,引导图启动结束,此时在用户偏好中做一个标记,并把这个标记存储起来
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setBool:YES forKey:@"daociyiyou"];
//赋值之后立即同步,作用是将事件标记的值存储起来
- (void)handleTap : (UITapGestureRecognizer *)tap{
NSLog(@"轻拍了嘛?");
//是一个单例,可以实现数据持久化,当用户点击到最后一张图片时,引导图启动结束,此时在用户偏好中做一个标记,并把这个标记存储起来
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setBool:YES forKey:@"daociyiyou"];
//赋值之后立即同步,作用是将事件标记的值存储起来
[user synchronize];
//实现点击最后一张图片进入用户进入用户界面
PageViewController *pageVC = [[PageViewController alloc]init];
//更换当前的window 的根视图控制器,将pageVC设置为主界面
//取出当前的应用程序对象
[UIApplication sharedApplication].keyWindow.rootViewController = pageVC;
[pageVC release];
}
#pragma mark scrollView的实现
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//求出scrollView内容在x轴偏移量
CGFloat x = _scrollView.contentOffset.x;
//求出pageCrontrol对应的下标
_pageControl.currentPage = x / kScreenWidth;
}
- (void)handlePage : (UIPageControl *)page{
NSLog(@"%ld",page.currentPage);
//让_scrollView 跟pageControl 页数的改变而改变
PageViewController *pageVC = [[PageViewController alloc]init];
//更换当前的window 的根视图控制器,将pageVC设置为主界面
//取出当前的应用程序对象
[UIApplication sharedApplication].keyWindow.rootViewController = pageVC;
[pageVC release];
}
#pragma mark scrollView的实现
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//求出scrollView内容在x轴偏移量
CGFloat x = _scrollView.contentOffset.x;
//求出pageCrontrol对应的下标
_pageControl.currentPage = x / kScreenWidth;
}
- (void)handlePage : (UIPageControl *)page{
NSLog(@"%ld",page.currentPage);
//让_scrollView 跟pageControl 页数的改变而改变
_scrollView.contentOffset = CGPointMake(page.currentPage * kScreenWidth, 0);
}
————————————————————————————————
AppDelegate.m
//根据判断条件让运行程序启动引导界面
//获取用户对象,用户对象中存储的是一些有用的偏好设置
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//获取用户对象,用户对象中存储的是一些有用的偏好设置
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ([user boolForKey:@"daociyiyou"] != NO) {
// //根视图控制器
self.window.rootViewController = [PageViewController new];
}else{
//根视图
self.window.rootViewController = [LaunchViewController new];
}
return YES;
//根视图
self.window.rootViewController = [LaunchViewController new];
}
return YES;
}
最终效果: