iOS UIScrollView的屬性

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;
}


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內容