UIKit 框架之<UIScrollView>

UIKit框架里面的几个视图非常重要,UIScrollViewUITableViewUICollectionView都是很常用。因为功能强大,所以很多朋友都会弄混淆一些概念,那么这篇文章就先从UIScrollView着手,全面剖析里面的各种属性和方法,这样用起来就会得心应手。

第一、UIScrollView的基本概念

是一个可以选择滑动的视图,用于显示更多的内容,可以通过手势放大或者缩小显示更多的内容。

有两个子类一个是UITableView,另一个是UITextView

第二、基本属性:


    scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320,250)] autorelease];

    scrollView.backgroundColor=[UIColor redColor];

    //设置内容的大小

    scrollView.contentSize=CGSizeMake(320*4,250);

    //当超出边界时表示是否可以反弹

    scrollView.bounces=YES;

    //是否分页

    scrollView.pagingEnabled=YES;

    //是否滚动

    scrollView.scrollEnabled=YES;

    //是否显示边界

    scrollView.showsHorizontalScrollIndicator=YES;

    //设置indicator的风格

    scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

    //提示用户

    [scrollView flashScrollIndicators];

    //设置内容的边缘

    scrollView.contentInset=UIEdgeInsetsMake(0,50, 50,0);

    

    [self.viewaddSubview:scrollView];

    

    UILabel *label=[[[UILabelalloc] initWithFrame:CGRectMake(320,210, 320,40)] autorelease];

    label.backgroundColor=[UIColoryellowColor];

    label.text=@"学习scroolView";

    [scrollViewaddSubview:label];

 //调转到指定的offset

    [scrollView setContentOffset:CGPointMake(320,0)];

利用UIPageControllerUIScrollView实现滚动图片


  1. 1 - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UIScrollView *ScrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)] autorelease];  
  5.     ScrollView.delegate=self;  
  6.     ScrollView.scrollsToTop=YES;  
  7.     ScrollView.contentSize=CGSizeMake(320*5, 180);  
  8.     ScrollView.backgroundColor=[UIColor clearColor];  
  9.     ScrollView.pagingEnabled=YES;  
  10.     ScrollView.showsHorizontalScrollIndicator=NO;  
  11.     self.tableView.tableHeaderView=ScrollView;  
  12.       
  13.     for (int index=0; index<5; index++) {  
  14.         UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(index*320, 0, 320, 180)];  
  15.        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",index+1]];  
  16.         
  17.         [ScrollView addSubview:imageView];  
  18.         [imageView release];  
  19.     }  
  20.       
  21.     UIPageControl *pageControl=[[[UIPageControl alloc] initWithFrame:CGRectMake(0, 150, 320, 30)] autorelease];  
  22.     [self.tableView addSubview:pageControl];  
  23.     pageControl.numberOfPages=5;  
  24.     pageControl.tag=1001;  
  25.   
  26. }  
  27.   
  28. 1pragma mark - Table view data source  
  29.   
  30. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  31. {  
  32.     return 1;  
  33. }  
  34.   
  35. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  36. {  
  37.     return 10;  
  38. }  
  39.   
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  41. {  
  42.     static NSString *CellIdentifier = @"Cell";  
  43.   
  44.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  45.     if (cell==nil) {  
  46.         cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  47.     }  
  48.     cell.textLabel.text=[NSString stringWithFormat:@"row: %d",indexPath.row];  
  49.       
  50.     return cell;  
  51. }  
  52. 1
  53. #pragma mark -UIScrollViewDelegate  
  54. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView  
  55. {  
  56.     if ([scrollView isMemberOfClass:[UITableView class]]) {  
  57.         NSLog(@"HELLO");  
  58.     }else  
  59.     {  
  60.         int current=scrollView.contentOffset.x/320;  
  61.         NSLog(@"scrollview :%f",scrollView.contentOffset.x);  
  62.         UIPageControl *pageControl=(UIPageControl*)[self.view viewWithTag:1001];  
  63.         pageControl.currentPage=current;  
  64.     }  
  65. }  


contenSize
顾名思义,表示内容的宽和高,从图上也能清晰的看到,就是表示一张图的宽和高。

contentInset
这个就是在contentSize外面加的白色的部分,图上看的清清楚楚。

contentOffset
这个属性是是个CGPoint类型,表示现在视图上看到的那个点,距离图片真实原点的位置。

因为contentOffset的那个点不是从contentInset的原点开始的,所以如果滑动到如图上所示的位置,于是就有了负值。


在滚动的过程中,实际上就是contentOffset的值在不断变化,当手指触摸后,UIScrollView会暂时拦截触摸事件,使用一个计时器。假如在计时器到点后没有发生手指移动事件,那么UIScrollView发送 tracking events 到被点击的 subview 上面。如果在计时器到点前发生了移动事件,那么UIScrollView取消 tracking 然后自己发生滚动。

可以重载子类

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;

决定自己是否接受 touch 事件

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

开始发送 tracking messages 给 subview 的时候调用这个方法,决定是否发送 tracking messages 消息到 subview。
返回NO -> 发送,表示不取消
返回YES -> 不发送,表示会取消

@property(nonatomic,readonly,getter=isTracking)  BOOL tracking;

当 touch 后还没有拖动的时候值是YES,否则NO

@property(nonatomic,readonly,getter=isZoomBouncing)  BOOL zoomBouncing;

当内容放大到最大或者最小的时候值是YES,否则NO

@property(nonatomic,readonly,getter=isZooming)  BOOL zooming;

当正在缩放的时候值是YES,否则NO

@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating;

当滚动后,手指放开但是还在继续滚动中。这个时候是YES,其他时候是NO

@property(nonatomic)  CGFloat decelerationRate

设置手指放开后的减速率

@property(nonatomicCGFloat maximumZoomScale;

表示放大的最大倍数

@property(nonatomicCGFloat minimumZoomScale;

表示缩小的最小倍数

@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;

当值为YES的时候,就会产生翻页那种效果

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;

决定是否可以滚动

@property(nonatomicBOOL delaysContentTouches;

当值为YES的时候,用户一旦触碰,然后再一定时间内没有移动,UIScrollView会发送 tracking events,然后用户移动手指足够长度触发滚动事件,这个时候,UIScrollView发送了-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event到 subview,然后UIScrollView开始滚动。假如值为NOUIScrollView发送 tracking events 后,就算用户移动手指,UIScrollView也不会滚动。

@property(nonatomic)  BOOL showsHorizontalScrollIndicator;

滚动时是否显示水平滚动条

@property(nonatomic)  BOOL showsVerticalScrollIndicator;

滚动时是否显示垂直滚动条

@property(nonatomic)  BOOL bounces;

默认是YES,就是滚动超过边界会有反弹回来的效果,如果设置为NO,那么滚动到边界就会立刻停止

@property(nonatomicBOOL bouncesZoom;

这个效果反映在缩放上面,如果缩放超过最大缩放,就会有反弹效果,加入设置为NO,则达到最大或者最小的时候立刻停止

@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;

默认是NO,可以在垂直和水平方向同时运动。当值为YES的时候,加入一开始是垂直或者水平运动,那么接下来会锁定另外一个方向的滚动。加入一开始是对角方向滚动,则不会禁止某个方向

</pre><br />@property(<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(133, 153, 0);">nonatomic</span>) UIScrollViewIndicatorStyle indicatorStyle;

滚动条的样式,基本只是设置颜色

@property(nonatomic)  UIEdgeInsets scrollIndicatorInsets;

设置滚动条的位置


#pragma mark UIScrollViewDelegate
//只要滚动了就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;   
{
//    NSLog(@" scrollViewDidScroll");
    NSLog(@"ContentOffset  x is  %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}
//开始拖拽视图
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;   
{
   NSLog(@"scrollViewWillBeginDragging");
}
//完成拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; 
{
   NSLog(@"scrollViewDidEndDragging");
}
//将开始降速时
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   
{
   NSLog(@"scrollViewWillBeginDecelerating");
}

//减速停止了时执行,手触摸时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;   
{
   NSLog(@"scrollViewDidEndDecelerating");
}

//滚动动画停止时执行,代码改变时出发,也就是setContentOffset改变时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
{
   NSLog(@"scrollViewDidEndScrollingAnimation");
}

//设置放大缩小的视图,要是uiscrollview的subview
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;   
{
   NSLog(@"viewForZoomingInScrollView");
    return viewA;
}

//完成放大缩小时调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale; 
{
    viewA.frame=CGRectMake(50,0,100,400);
   NSLog(@"scale between minimum and maximum. called after any 'bounce' animations");
}// scale between minimum and maximum. called after any 'bounce' animations

//如果你不是完全滚动到滚轴视图的顶部,你可以轻点状态栏,那个可视的滚轴视图会一直滚动到顶部,那是默认行为,你可以通过该方法返回NO来关闭它
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   
{
    NSLog(@"scrollViewShouldScrollToTop");
   returnYES;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;     
{
    NSLog(@"scrollViewDidScrollToTop");
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值