一、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.pagingEnabled = YES;//默認爲NO
//2.8 設置當滑動到屏幕邊緣的時候是否出現反彈效果
scrollView.bounces = YES;//默認爲YES
//2.9 設置當內容區域等於或小於可視區域時,依然具備邊界反彈效果
//水平方向
scrollView.alwaysBounceHorizontal = YES;//默認爲NO
//垂直方向
scrollView.alwaysBounceVertical = YES;//默認爲NO
//2.10 設置點擊狀態欄,scrollView 是否回到頂部,此時y軸的偏移量是0
scrollView.scrollsToTop = YES;//默認爲YES
//2.11 scrollView 的代理屬性
//self指試圖控制器對象
scrollView.delegate = self;
//2.12 設置scrollView的縮放比例
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];
}
——————————————————————————-——————
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的偏移量
// 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的使用技巧
使用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: )];
[imageView addGestureRecognizer:tap];
[tap release];
//打開用戶交互
imageView.userInteractionEnabled = YES;
}
}
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];
//配置其餘點的顏色
_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"];
//賦值以後當即同步,做用是將事件標記的值存儲起來
[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 頁數的改變而改變
_scrollView.contentOffset = CGPointMake(page.currentPage * kScreenWidth, 0);
}
————————————————————————————————
AppDelegate.m
//根據判斷條件讓運行程序啓動引導界面
//獲取用戶對象,用戶對象中存儲的是一些有用的偏好設置
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ([user boolForKey:@"daociyiyou"] != NO) {
// //根視圖控制器
self.window.rootViewController = [PageViewController new];
}else{
//根視圖
self.window.rootViewController = [LaunchViewController new];
}
return YES;
}
最終效果: