1、UIScorollView 是ios中提供的滑动控件,用来解决当内容区域大于scorollView可视区域时,可以通过滑动的方式查看整个内容区域,UIScorollView 的滑动控件的基类,后期药学的UITableView(表视图),内部实现的原理是修改视图的bounds
//1.创建对象
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 设置是否整屏滚动
//
//
//水平方向
//垂直方向
}
——————————————————————————-——————
scrollView 的代理属性的13个方法及用途
//触发时机:ScrollView 将要开始拖拽idea时候触发(此时手指刚刚触摸到scrollView的时候)
- (void)scrollViewWillBeginDragging:( UIScrollView *)scrollView{
NSLog(@"开始拖拽了没?");
}
//触发时机:ScrollView 将要结束拖拽的时候触发
- (void)scrollViewWillEndDragging:( UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_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)scrollViewWillBeginDragg
}
//触发时机:ScrollView 将要结束拖拽的时候触发
- (void)scrollViewWillEndDraggin
}
- (void)scrollViewDidEndDragging
}
//触发时机:将要减速的时候触发(此方法不一定触发,匀速不一定触发)
- (void)scrollViewWillBeginDecel
}
//触发时机:已经结束减速的时候触发(此方法不一定触发,上面的减速方法不触发,这个结束减速的方法就不会触发)
- (void)scrollViewDidEndDecelera
// 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 *)view NS_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)scrollViewDidEndScrollin
}
//触发时机,返回缩放后的视图,但是只能返回scrollView(内容)上的子视图
- (UIView *)viewForZoomingInScrollVi
}
//触发时机:开始缩放的时候触发
- (void)scrollViewWillBeginZoomi
}
//触发时机:结束缩放的时候触发
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
}
//触发时机:点击状态栏,调用该方法,该方法能实现的前提是scrollToTop的属性是YES
- (BOOL)scrollViewShouldScrollTo
}
//触发时机:scrollView 已经回到顶部了
- (void)scrollViewDidScrollToTop
}
==============================================================
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:self action:@selector(handleTap: )];
#import "PageViewController.h"
//图片个数
#define kImageCount 6
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight
//定义属性方便使用
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIPageControl *pageControl;
@end
@implementation LaunchViewController
- (void)dealloc{
}
- (void)viewDidLoad {
}
- (void)layoutScrollView{
}
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{
}
#pragma mark 是轻拍手势的实现
- (void)handleTap : (UITapGestureRecognizer *)tap{
NSLog(@"轻拍了嘛?");
//是一个单例,可以实现数据持久化,当用户点击到最后一张图片时,引导图启动结束,此时在用户偏好中做一个标记,并把这个标记存储起来
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setBool:YES forKey:@"daociyiyou"];
//赋值之后立即同步,作用是将事件标记的值存储起来
- (void)handleTap : (UITapGestureRecognizer *)tap{
}
- (void)scrollViewDidEndDecelera
}
- (void)handlePage : (UIPageControl *)page{
}
————————————————————————————————
AppDelegate.m
}
最终效果:
本节所需素材下载:
http://pan.baidu.com/s/1dD8pMLj
__________________________________________________________________________________
欢迎学习本文,未经博主许可,禁止转载!