iOS中scrollview自动滚动的实现

本文介绍如何通过使用setContentOffset:animated:方法在iOS应用中实现UIScrollView的自动滚动,包括开场动画效果和定时器实现缓慢滚动的技巧。

原问题是,我要展现给用户的内容放在scrollview中,让内容从上到底自动滚动,我最开始用的是DDAutoscrollview,但是无法实现。

一种解决方案见下边,更多解决方案见:http://ask.youkuaiyun.com/questions/374

 

.h文件

[plain] view plain copy
  1.  @interface Interface1 : UIViewController {  
  2.   
  3.     IBOutlet UIScrollView *scroller;  
  4.     IBOutlet UILabel *warnung;  
  5.   
  6. }  
  7.   
  8.   
  9. @property (nonatomic, retain) IBOutlet UIScrollView* scrollView;  


.m文件

[plain] view plain copy
  1. - (void)viewDidAppear:(BOOL)animated  
  2. {  
  3.     [super viewDidAppear:animated];  
  4.     CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);  
  5.     [self.scrollView setContentOffset:bottomOffset animated:NO];  
  6.   
  7.     CGPoint newOffset = self.scrollView.contentOffset;  
  8.     newOffset.y = 0;  
  9.     [self.scrollView setContentOffset:newOffset animated:YES];  
  10. }  
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.   
  15.     [scroller setScrollEnabled:YES];  
  16.     [scroller setContentSize:CGSizeMake(320, 420)];  
  17.   
  18.         [super viewDidLoad];  
  19. }  


使用setContentOffset:animated:

[plain] view plain copy
  1. UIScrollView *scrollView = ...;  
  2. CGPoint newOffset = scrollView.contentOffset;  
  3. newOffset.y = 0;  
  4. [scrollView setContentOffset:newOffset animated:YES];  


如果需要开场动画的效果,在scrollView的viewcontroller实现

[plain] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     // ...  
  6.   
  7.     CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);  
  8.     [self.scrollView setContentOffset:bottomOffset animated:NO];  
  9. }  
  10. - (void)viewDidAppear:(BOOL)animated  
  11. {  
  12.     [super viewDidAppear:animated];  
  13.   
  14.     CGPoint newOffset = self.scrollView.contentOffset;  
  15.     newOffset.y = 0;  
  16.     [self.scrollView setContentOffset:newOffset animated:YES];  
  17. }  


移动的慢点,用timer实现:

[plain] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     // ...  
  6.   
  7.     CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);  
  8.     [self.scrollView setContentOffset:bottomOffset animated:NO];  
  9. }  
  10.   
  11.   
  12. - (void)viewDidAppear:(BOOL)animated  
  13. {      
  14.     [super viewDidAppear:animated];  
  15.   
  16.     CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);  
  17.   
  18.     //设置延迟时间  
  19.     float scrollDurationInSeconds = 4.0;  
  20.   
  21.     //计算timer间隔  
  22.   
  23.   
  24.     float totalScrollAmount = bottomOffset.y;  
  25.     float timerInterval = scrollDurationInSeconds / totalScrollAmount;  
  26.   
  27.     [NSTimer scheduledTimerWithTimeInterval:timerInterval target:self selector:@selector(scrollScrollView:) userInfo:nil repeats:YES];  
  28. }  
  29.   
  30. - (void)scrollScrollView:(NSTimer *)timer  
  31. {  
  32.     CGPoint newScrollViewContentOffset = self.scrollView.contentOffset;  
  33.   
  34.     //向上移动 1px  
  35.     newScrollViewContentOffset.y -= 1;  
  36.   
  37.   
  38.     newScrollViewContentOffset.y = MAX(0, newScrollViewContentOffset.y);  
  39.   
  40.     //如果到顶了,timer中止  
  41.     if (newScrollViewContentOffset.y == 0) {  
  42.         [timer invalidate];  
  43.     }  
  44.   
  45.     //最后设置scollView's contentOffset  
  46.     self.scrollView.contentOffset = newScrollViewContentOffset;  
  47. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值