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   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 *)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

@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 ()

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


你可能感兴趣的:(iOS UIScrollView的属性)