iOS界面編程-UIScrollView

一、介紹

   UIScrollView繼承自UIView,它爲大於窗口大小的內容提供顯示支持。用戶可以通過滑動屏幕滾動其它內容還以通過手指捏放大縮小顯示內容。UIScroll是一些UIKit的超類,包括UITableView及UITextView

二、相關方法和屬性

1.管理內容的顯示

a .設置contentView的相對於scrollView的偏移量,可以理解爲scrollview當前顯示區域頂點相對於frame頂點的偏移量。

  - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

b、設置scrollView 內容視圖大小,即可顯示的區域。

@property(nonatomic) CGSize contentSize

c、@property(nonatomic) UIEdgeInsets contentInset

contentInset 是scrollview中contentView.frame.origin與scrollview.frame.origin的關係,比如contentView的frame爲(0,30,320,480),那麼contentInset則爲(0, 30),

2.管理滾動

a、@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled 可設置是否可以滾動,默認爲yes

b.@property(nonatomic, getter=isDirectionalLockEnabled) BOOL directionalLockEnabled ,設置爲YES滾動某個方向時,另一個方向不能滾動。默認爲NO,兩個方向都可以同時滾動

c、@property(nonatomic) BOOL scrollsToTop 布爾值判斷滑動到頂點的手勢是否可用。

d.- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated指定座標空間可見

e。@property(nonatomic, getter=isPagingEnabled) BOOL pagingEnabled 默認NO,當設置爲YES時,可用

f、@property(nonatomic) BOOL bounces,設置是否有反彈效果,默認爲YES

g、@property(nonatomic) BOOL alwaysBounceVertical 設置即使內容小於scrollveiw ,水平方向總是有反彈效果,默認是NO

h、@property(nonatomic) BOOL alwaysBounceHorizontal設置即使內容小於scrollveiw ,垂直方向總是有反彈效果,默認是NO
 

可以由子類繼承,當在顯示內容按下手指時,定製默認的行爲。

- (BOOL)touchesShouldBegin:(NSSet<UITouch *> * nonnull)touches withEvent:(UIEvent * nullable)event inContentView:(UIView * nonnull)view

- (BOOL)touchesShouldCancelInContentView:(UIView * nonnull)view

@property(nonatomic) BOOL canCancelContentTouches

@property(nonatomic) BOOL delaysContentTouches

@property(nonatomic) CGFloat decelerationRate

@property(nonatomic, readonly, getter=isDragging) BOOL dragging

@property(nonatomic, readonly, getter=isTracking) BOOL tracking 判斷用戶已經按下還沒有開始拖

@property(nonatomic, readonly, getter=isDecelerating) BOOL decelerating 判斷用戶是否拖拽結束,但滾動還沒有結束


3、管理滑動指示器

a指示器類型@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle有三種類型

b指示器@property(nonatomic) UIEdgeInsets scrollIndicatorInsets

c@property(nonatomic) BOOL showsHorizontalScrollIndicator 是否顯示水平指示器,默認YES

d@property(nonatomic) BOOL showsVerticalScrollIndicator 是否顯示垂直指示器,默認NO

e- (void)flashScrollIndicators

@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer

@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

@property(nonatomic) CGFloat zoomScale

- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated

@property(nonatomic) CGFloat maximumZoomScale

@property(nonatomic) CGFloat minimumZoomScale

4、管理委託

@property(nonatomic, weak) id< UIScrollViewDelegate > delegate

5、管理鍵盤

表明拖拽過程中,鍵盤消失的模式@property(nonatomicUIScrollViewKeyboardDismissMode keyboardDismissMode

三實際例子

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(50, 80, 150, 300)];
    scrollView.backgroundColor = [UIColor grayColor];
    [scrollView setContentOffset:CGPointMake(14, 14) animated:YES];
    
    scrollView.contentSize=CGSizeMake(600, 500);
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  150, 500)];
    contentView.backgroundColor = [UIColor greenColor];
    
    [scrollView setContentOffset:CGPointMake(30, 0) animated:YES];
    
    [scrollView setContentInset:UIEdgeInsetsMake(10, 10, 0, 0)];
//    scrollView.scrollEnabled = NO;
//    scrollView.directionalLockEnabled = YES;
    
    [scrollView scrollRectToVisible:CGRectMake(300, 100, 50, 50) animated:YES];
    [scrollView addSubview:contentView];
//    scrollView.bounces= NO;
    [self.view addSubview:scrollView]

  _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 64, scrollViewWidth, scrollViewHeight)];
    _scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(scrollViewWidth * 5, scrollViewHeight);
    _scrollView.pagingEnabled = YES;
    _scrollView.maximumZoomScale = 3;
    _scrollView.minimumZoomScale = 0.5;
    _scrollView.delegate = self;
    
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth * 5, scrollViewHeight)];
    _contentView.backgroundColor = [UIColor greenColor];
    [_scrollView addSubview:_contentView];

    for (int i = 0; i < 5; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * scrollViewWidth , 0, scrollViewWidth, scrollViewHeight)];
        if (i % 2 ==0) {
            view.backgroundColor = [UIColor blackColor];
        }else {
            view.backgroundColor = [UIColor whiteColor];
        }
        [_contentView addSubview:view];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
        imageView.backgroundColor = [UIColor clearColor];
        NSString *imageName = [NSString stringWithFormat:@"bg%i.png", i+1];
        imageView.image = [UIImage imageNamed:imageName];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imageView];



發佈了67 篇原創文章 · 獲贊 20 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章