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阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容