UIScrollView基本用法

来源:互联网 发布:如何查看windows版本 编辑:程序博客网 时间:2024/06/06 00:07

1、UIScrollView可以提供在屏幕尺寸不够的情况下滑动浏览大区域画面

基本用法如下:

[objc] view plaincopy
  1. UIScrollView * scrollView = [[UIScrollView alloc] init];  
  2. scrollView.frame = CGRectMake(0100, width, height - 300);//UIScrollView大小  
  3. [self.view addSubview:scrollView];  
  4. scrollView.backgroundColor = [UIColor orangeColor];  
  5.   
  6. UIImage * image = [UIImage imageNamed:@"yuan.png"];  
  7. UIImageView * imageView = [[UIImageView alloc] initWithImage:image];  
  8. [scrollView addSubview:imageView];  
  9.   
  10. //自动调整宽高  
  11. scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  
  12. scrollView.contentSize = imageView.bounds.size;  //重要,这个是设置UIScrollView滚动区域的大小,否则不能滚动  

2、滚动条样式:

默认:灰色线包围黑色条

[objc] view plaincopy
  1. scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;  

UIScrollViewIndicatorStyleBlack : 黑色条

UIScrollViewIndicatorStyleWhite : 白色条

样式枚举:

[objc] view plaincopy
  1. typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {  
  2.     UIScrollViewIndicatorStyleDefault,     // black with white border. good against any background  
  3.     UIScrollViewIndicatorStyleBlack,       // black only. smaller. good against a white background  
  4.     UIScrollViewIndicatorStyleWhite        // white only. smaller. good against a black background  
  5. };  

3、放大缩小:

须遵守UIScrollViewDelegate协议

[objc] view plaincopy
  1. scrollView.delegate = self;  
  2. scrollView.minimumZoomScale = 0.5;  
  3. scrollView.maximumZoomScale = 2.0;  
还需要实现如下方法:

[objc] view plaincopy
  1. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {  
  2.     for (id subView in scrollView.subviews) {  
  3.         if ([subView isKindOfClass:[UIImageView class]]) {  
  4.             return subView;  
  5.         }  
  6.     }  
  7.     return nil;  
  8. }  

最大放大两倍,最小缩小一倍

4、以页为单位滚动:

[objc] view plaincopy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     // Do any additional setup after loading the view, typically from a nib.  
  4.     self.view.backgroundColor = [UIColor whiteColor];  
  5.       
  6.     CGFloat width = [[UIScreen mainScreen] bounds].size.width;  
  7.     CGFloat height = [[UIScreen mainScreen] bounds].size.height;  
  8.       
  9.       
  10.     UIScrollView * scrollView = [[UIScrollView alloc] init];  
  11.     scrollView.frame = CGRectMake(0100, width, height - 300);//UIScrollView大小  
  12.     [self.view addSubview:scrollView];  
  13.     scrollView.backgroundColor = [UIColor orangeColor];  
  14.       
  15.   
  16.     UIView * view1 = [[UIView alloc] init];  
  17.     view1.frame = scrollView.bounds;  
  18.     view1.backgroundColor = [UIColor purpleColor];  
  19.     [scrollView addSubview:view1];  
  20.       
  21.     UIView * view2 = [[UIView alloc] init];  
  22.     view2.frame = CGRectMake(width, 0, width, scrollView.frame.size.height);  
  23.     view2.backgroundColor = [UIColor redColor];  
  24.     [scrollView addSubview:view2];  
  25.       
  26.     UIView * view3 = [[UIView alloc] init];  
  27.     view3.frame = CGRectMake(width * 20, width, scrollView.frame.size.height);  
  28.     view3.backgroundColor = [UIColor greenColor];  
  29.     [scrollView addSubview:view3];  
  30.       
  31.     scrollView.pagingEnabled = YES;  
  32.     scrollView.showsHorizontalScrollIndicator = NO//隐藏横向滚动条  
  33.     scrollView.showsVerticalScrollIndicator = NO//隐藏竖向滚动条  
  34.       
  35.     scrollView.contentSize = CGSizeMake(width * 3, scrollView.frame.size.height);  
  36.       
  37.           
  38. }  

5、UIScrollViewDelegate

[objc] view plaincopy
  1. @protocol UIScrollViewDelegate<NSObject>  
  2.   
  3. @optional  
  4.   
  5. //滚动时调用,可以实时监测滚动变化  
  6. - (void)scrollViewDidScroll:(UIScrollView *)scrollView;                                               // any offset changes  
  7.   
  8. //实时监测缩放  
  9. - (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes  
  10.   
  11. //开始拖动的时候调用  
  12. // called on start of dragging (may require some time and or distance to move)  
  13. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;  
  14.   
  15. //停止拖动的时候调用(注:停止拖动时,拖动效果会持续一段,慢慢停下)  
  16. // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest  
  17. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);  
  18.   
  19.   
  20. //结束拖动时调用  
  21. // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards  
  22. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;  
  23.   
  24. //开始减速的时候调用  
  25. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   // called on finger up as we are moving  
  26.   
  27. //结束减速的时候调用  
  28. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt  
  29.   
  30. //设置scrollview动画,动画结束后调用  
  31. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating  
  32.   
  33.   
  34. //返回缩放的可以view,如上例UIImageView  
  35. - (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;     // return a view that will be scaled. if delegate returns nil, nothing happens  
  36.   
  37. //开始缩放的时候调用  
  38. - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content  
  39.   
  40. //结束缩放的时候调用  
  41. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations  
  42.   
  43.   
  44. //是否可以拖动到顶部,yes可以否则不可以  
  45. - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   // return a yes if you want to scroll to the top. if not defined, assumes YES  
  46.   
  47. //可以拖动到顶部时,拖动结束后调用  
  48. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      // called when scrolling animation finished. may be called immediately if already at top  
  49.   
  50. @end  
0 0
原创粉丝点击