要讓一組視圖View一塊兒滾動,就須要滾動視圖。在Android上提供了ScrollView,而iOS則提供了UIScrollView。UIScrollView比Android上的ScrollView多出3種特性:bash
前2個特性是Android項目中一直費盡心思想實現的功能,但都不理想,或者還有手勢衝突等問題。第三個特性通常用ViewPager來作,而不是用ScrollView,而且還自帶複用功能。那UIScrollView能夠算是沒有複用功能的ViewPager了。本篇就來看看這個牛逼的UIScrollView吧。動畫
#import "ViewController.h"
@interface ViewController ()
/**
* 滾動視圖
*/
@property(retain,nonatomic) UIScrollView* scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//獲取控制器View的寬、高
CGFloat vcViewWidth = self.view.frame.size.width;
CGFloat vcViewHeight = self.view.frame.size.height;
//建立UIScrollView
_scrollView = [[UIScrollView alloc] init];
//設置UIScrollView的位置和寬高爲控制器View的寬高
_scrollView.frame = CGRectMake(0, 0, vcViewWidth, vcViewHeight);
//設置畫布大小,通常比frame大,這裏設置橫向能拖動4張圖片的範圍
_scrollView.contentSize = CGSizeMake(vcViewWidth * 4, vcViewHeight);
//將ScrollView添加到控制器的View上
[self.view addSubview:_scrollView];
}
@end
複製代碼
通過上面的設置,咱們的UIScrollView已經添加到屏幕中了,可是尚未內容,接下來咱們給UIScrollView添加4張圖片,分別橫向排列。atom
//建立圖片ImageView添加到ScrollView中
for(int i = 0; i < 4; i++) {
NSString* imgName = [NSString stringWithFormat:@"%d.jpg", i + 1];
UIImage* img = [UIImage imageNamed:imgName];
UIImageView* imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(vcViewWidth * i, 0, vcViewWidth, vcViewHeight);
[_scrollView addSubview:imgView];
}
複製代碼
_scrollView.scrollEnabled = YES;
複製代碼
_scrollView.bounces = YES;
複製代碼
//開啓橫向彈動效果
_scrollView.alwaysBounceHorizontal = YES;
//關閉縱向彈動效果
_scrollView.alwaysBounceVertical = NO;
複製代碼
_scrollView.pagingEnabled = YES;
複製代碼
//隱藏橫向滾動條
_scrollView.showsHorizontalScrollIndicator = NO;
//隱藏豎向滾動條
_scrollView.showsVerticalScrollIndicator = NO;
複製代碼
UIScrollView的代理,就是設置UIScrollView上的事件回調的協議,通常咱們讓當前控制器來實現協議。spa
文件:ViewController.h
@interface ViewController : UIViewController<UIScrollViewDelegate>
@end
複製代碼
文件:ViewController.m
#import "ViewController.h"
@interface ViewController ()
/**
* 滾動視圖
*/
@property(retain,nonatomic) UIScrollView* scrollView;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//...省略上面提到的設置
//將ScrollView添加到控制器的View上
[self.view addSubview:_scrollView];
//設置代理
_scrollView.delegate = self;
}
//滾動視圖移動時回調
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"視圖移動 x: %f", scrollView.contentOffset.x);
}
//滾動視圖結束拖動時回調
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"視圖結束拖動");
}
//滾動視圖即將開始拖動時回調
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
NSLog(@"滾動視圖即將開始拖動");
}
//滾動視圖結束拖動時回調
- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSLog(@"滾動視圖結束拖動");
}
//視圖即將減速時調用
- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
NSLog(@"視圖即將減速");
}
//視圖已經結束減速時回調
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"視圖已經結束減速");
}
@implementation ViewController
複製代碼
self.scrollView.contentOffset = CGPointMake(0, 0);
//或者
[self.scrollView scrollRectToVisible:CGRectMake(0, 0, 300, 400) animated:NO];
複製代碼
[self.scrollView scrollRectToVisible:CGRectMake(0, 0, 300, 400) animated:YES];
複製代碼