UIScrollView是一個可以滾動的view,所以經常利用UIScrollView做循環滾動,下面介紹一下UIScrollView的一些常用屬性:
//視圖的偏移量
@property(nonatomic) CGPoint contentOffset; // default CGPointZero
//設置內容區域的大小,只有設置大于視圖本身才能滾動
@property(nonatomic) CGSize contentSize; // default CGSizeZero
//設置距離上左下右的偏移
@property(nonatomic) UIEdgeInsets contentInset; // default UIEdgeInsetsZero. add additional scroll area around content
//設置點擊邊界是否回彈
@property(nonatomic) BOOL bounces; // default YES. if YES, bounces past edge of content and back again
//視圖的偏移量
@property(nonatomic) CGPoint contentOffset; // default CGPointZero
//設置內容區域的大小,只有設置大于視圖本身才能滾動
@property(nonatomic) CGSize contentSize; // default CGSizeZero
//設置scrollView的內邊距,也就是內容視圖邊緣和scrollView的邊緣的留空距離
@property(nonatomic) UIEdgeInsets contentInset; // default UIEdgeInsetsZero. add additional scroll area around content
//設置代理
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate; // default nil. weak reference
//本來就是用來讓用戶每次只在一個方向上滾動,豎直或者水平,但是如果初始移動方向處于45°左右的時候,這個鎖就失效了。
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled; // default NO. if YES, try to lock vertical or horizontal scrolling while dragging
//設置點擊邊界是否能夠回彈
@property(nonatomic) BOOL bounces; // default YES. if YES, bounces past edge of content and back again
//設置點擊垂直邊框是否回彈
@property(nonatomic)BOOL alwaysBounceVertical; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
//設置點擊水平邊框是否回彈
@property(nonatomic) BOOL alwaysBounceHorizontal; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally
//設置滾動是否翻頁功能
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled __TVOS_PROHIBITED;// default NO. if YES, stop on multiples of view bounds
//設置是否能夠滾動
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled; // default YES. turn off any dragging temporarily
//是否顯示水平滾動條
@property(nonatomic) BOOL showsHorizontalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
//是否顯示豎直滾動條
@property(nonatomic) BOOL showsVerticalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
//狀態條和scrollView邊距的距離
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets; // default is UIEdgeInsetsZero. adjust indicators inside of insets
//狀態條的風格,默認值為UIScrollViewIndicatorStyleDefault。除此之外,還有UIScrollViewIndicatorStyleBlack, UIScrollViewIndicatorStyleWhite兩種其他風格。可以用來和環境配色
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; // default is UIScrollViewIndicatorStyleDefault
//減速的速率
@property(nonatomic) CGFloat decelerationRate NS_AVAILABLE_IOS(3_0);
//設置偏移量 是否帶動畫
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated; // animate at constant velocity to new offset
//視圖是矩形邊緣可見
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // scroll so rect is just visible (nearest edges). nothing if rect completely visible
//短暫的顯示一下狀態條,當你將scrollView調整到最上面時,需要調用一下該方法
- (void)flashScrollIndicators; // displays the scroll indicators for a short time. This should be done whenever you bring the scroll view to front.
//只讀,用戶開始觸摸視圖(也許還沒有開始拖動),該屬性值為YES
@property(nonatomic,readonly,getter=isTracking) BOOL tracking; // returns YES if user has touched. may not yet have started dragging
//只讀,當用戶開始拖動(手指已經在屏幕上滑動一段距離),該屬性值為YES
@property(nonatomic,readonly,getter=isDragging) BOOL dragging; // returns YES if user has started scrolling. this may require some time and or distance to move to initiate dragging
// 只讀,當用戶松開手指,但視圖仍在滾動時,該值返回YES
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating; // returns YES if user isn't dragging (touch up) but scroll view is still moving
//是否推遲觸屏手勢處理,默認值為YES。設置為YES的時候,系統在確定是否發生scroll事件之后,才會處理觸屏手勢,否則,則會立即調用touchesShouldBegin:withEvent:inContentView:方法
@property(nonatomic) BOOL delaysContentTouches; // default is YES. if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:. this has no effect on presses
//是否取消手勢處理,默認值為YEStouchesShouldBegin:withEvent:inContentView:方法
@property(nonatomic) BOOL canCancelContentTouches; // default is YES. if NO, then once we start tracking, we don't try to drag if the touch moves. this has no effect on presses
//點擊觸發事件
- (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event inContentView:(UIView *)view;
//取消點擊
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;
//最小比例
@property(nonatomic) CGFloat minimumZoomScale; // default is 1.0
//最大比例
@property(nonatomic) CGFloat maximumZoomScale; // default is 1.0. must be > minimum zoom scale to enable zooming
//當前比例
@property(nonatomic) CGFloat zoomScale NS_AVAILABLE_IOS(3_0); // default is 1.0
//設置比例大小
- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
//將內容縮放到rect中
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
//縮放超過縮放比例時,是否bounce,默認值為YES。如果值為NO,則達到最大或最小縮放比例時會立即停止縮放。否則,產生彈簧效果
@property(nonatomic) BOOL bouncesZoom; // default is YES. if set, user can go past min/max zoom while gesturing and the zoom will animate to the min/max value at gesture end
//只讀,用戶是否正在進行縮放手勢
@property(nonatomic,readonly,getter=isZooming) BOOL zooming; // returns YES if user in zoom gesture
//只讀,當縮放超過最大或者最小范圍的時候,回彈到最大最小范圍的過程中,該值返回YES。
@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomBouncing; // returns YES if we are in the middle of zooming back to the min/max value
//是否啟動點擊回彈到頂部
@property(nonatomic) BOOL scrollsToTop __TVOS_PROHIBITED; // default is YES.
//手勢
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);d.
@property(nullable, nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);
@property(nonatomic, readonly) UIGestureRecognizer *directionalPressGestureRecognizer UIKIT_AVAILABLE_TVOS_ONLY(9_0);
// 當拖動發生時,鍵盤的消失模式,默認值是不消失
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0); // default is UIScrollViewKeyboardDismissModeNone
@protocol UIScrollViewDelegate<NSObject>
@optional
////scrollView正在滾動
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes
////scrollView正在放大或縮小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes
// 開始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
//// 將要結束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
//已經結束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
//將開始減速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // called on finger up as we are moving
//減速完成,視圖停止滾動
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; // called when scroll view grinds to a halt
//滾動動畫已經停止執行
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
//設置放大縮小的視圖,是UIScrollView的Subview
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; // return a view that will be scaled. if delegate returns nil, nothing happens
//視圖將要開始放大或縮小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content
//視圖完成放大或縮小
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations
//輕點狀態欄,滾動視圖會一直滾動到頂部,那是默認行為YES,你可以通過該方法返回NO來關閉它
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; // return a yes if you want to scroll to the top. if not defined, assumes YES
//視圖已經滾動到頂部調用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView; // called when scrolling animation finished. may be called immediately if already at top
例如:視圖循環滾動的效果
#define WIDTH [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) UIPageControl *pageControl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageArray = @[@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg"];
[self.view addSubview:self.scrollView];
[self addImageToImageView];
}
- (void)addImageToImageView {
[self.scrollView setContentSize:CGSizeMake((self.imageArray.count+2)*WIDTH, self.view.frame.size.height*0.5)];
[self.scrollView setContentOffset:CGPointMake(WIDTH, 0)];
for (int i = 0; i < self.imageArray.count+2; i++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*WIDTH, 0, WIDTH, self.scrollView.frame.size.height)];
if (i == 0) {
imgView.image = [UIImage imageNamed:[self.imageArray lastObject]];
}else if(i == self.imageArray.count+1){
imgView.image = [UIImage imageNamed:[self.imageArray firstObject]];
}else {
imgView.image = [UIImage imageNamed:self.imageArray[i-1]];
}
[self.scrollView addSubview:imgView];
}
[self.view addSubview:self.pageControl];
}
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height*0.2, WIDTH, self.view.frame.size.height*0.5)];
_scrollView.backgroundColor = [UIColor orangeColor];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
}
return _scrollView;
}
- (UIPageControl *)pageControl {
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH*0.2, self.view.frame.size.height*0.7-20, WIDTH*0.6, 20)];
_pageControl.backgroundColor = [UIColor redColor];
_pageControl.currentPage = 0;
_pageControl.numberOfPages = self.imageArray.count;
_pageControl.pageIndicatorTintColor = [UIColor blueColor];//圓點的顏色
_pageControl.currentPageIndicatorTintColor = [UIColor greenColor];//當前圓點的顏色
}
return _pageControl;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//減速完成,視圖停止滾動
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x <= 0.0) {
[scrollView setContentOffset:CGPointMake(WIDTH*self.imageArray.count, 0)];
}
if (scrollView.contentOffset.x >= self.scrollView.contentSize.width-WIDTH){
[scrollView setContentOffset:CGPointMake(WIDTH, 0)];
}
self.pageControl.currentPage = (scrollView.contentOffset.x-WIDTH)/WIDTH;
}