您的位置:首页 > 产品设计 > UI/UE

UIKit 框架之<UIScrollView>

2016-03-30 22:55 274 查看
UIKit
框架里面的几个视图非常重要,
UIScrollView
UITableView
UICollectionView
都是很常用。因为功能强大,所以很多朋友都会弄混淆一些概念,那么这篇文章就先从
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)];利用UIPageController和UIScrollView实现滚动图片1 - (void)viewDidLoad  {      [super viewDidLoad];      UIScrollView *ScrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)] autorelease];      ScrollView.delegate=self;      ScrollView.scrollsToTop=YES;      ScrollView.contentSize=CGSizeMake(320*5, 180);      ScrollView.backgroundColor=[UIColor clearColor];      ScrollView.pagingEnabled=YES;      ScrollView.showsHorizontalScrollIndicator=NO;      self.tableView.tableHeaderView=ScrollView;            for (int index=0; index<5; index++) {          UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(index*320, 0, 320, 180)];         imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",index+1]];                  [ScrollView addSubview:imageView];          [imageView release];      }            UIPageControl *pageControl=[[[UIPageControl alloc] initWithFrame:CGRectMake(0, 150, 320, 30)] autorelease];      [self.tableView addSubview:pageControl];      pageControl.numberOfPages=5;      pageControl.tag=1001;    }    1pragma mark - Table view data source    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  {      return 1;  }    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {      return 10;  }    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {      static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];      if (cell==nil) {          cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      }      cell.textLabel.text=[NSString stringWithFormat:@"row: %d",indexPath.row];            return cell;  }  1#pragma mark -UIScrollViewDelegate  - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView  {      if ([scrollView isMemberOfClass:[UITableView class]]) {          NSLog(@"HELLO");      }else      {          int current=scrollView.contentOffset.x/320;          NSLog(@"scrollview :%f",scrollView.contentOffset.x);          UIPageControl *pageControl=(UIPageControl*)[self.view viewWithTag:1001];          pageControl.currentPage=current;      }  }  
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(nonatomic) CGFloat maximumZoomScale;
表示放大的最大倍数
@property(nonatomic) CGFloat minimumZoomScale;
表示缩小的最小倍数
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;
当值为
YES
的时候,就会产生翻页那种效果
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
决定是否可以滚动
@property(nonatomic) BOOL delaysContentTouches;
当值为
YES
的时候,用户一旦触碰,然后再一定时间内没有移动,
UIScrollView
会发送tracking events,然后用户移动手指足够长度触发滚动事件,这个时候,
UIScrollView
发送了
-(void)touchesCancelled:(NSSet
*)touches withEvent:(UIEvent *)event
到 subview,然后
UIScrollView
开始滚动。假如值为
NO
UIScrollView
发送tracking events 后,就算用户移动手指,
UIScrollView
也不会滚动。
@property(nonatomic)  BOOL showsHorizontalScrollIndicator;
滚动时是否显示水平滚动条
@property(nonatomic)  BOOL showsVerticalScrollIndicator;
滚动时是否显示垂直滚动条
@property(nonatomic)  BOOL bounces;
默认是
YES
,就是滚动超过边界会有反弹回来的效果,如果设置为
NO
,那么滚动到边界就会立刻停止
@property(nonatomic) BOOL 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

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  httpssegmentfault.co ui