Implementation of ScrollView Auto-scrolling in iOS

Source: Internet
Author: User

http://bbs.csdn.net/topics/390347330

The original problem is, I want to show the user's content in the ScrollView, so that the content from the end of the automatic scrolling, I started with the Ddautoscrollview, but can not be achieved.

One solution see below, more solutions see: http://ask.csdn.net/questions/374

. h file

Objective C Code?
123456789  @interfaceInterface1 : UIViewController{     IBOutletUIScrollView*scroller;    IBOutletUILabel*warnung;}@property(nonatomicretainIBOutletUIScrollView* scrollView;


. m file

Objective C Code?
12345678910111213141516171819 - (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);    [self.scrollView setContentOffset:bottomOffset animated:NO];    CGPoint newOffset = self.scrollView.contentOffset;    newOffset.y = 0;    [self.scrollView setContentOffset:newOffset animated:YES];}- (void)viewDidLoad{    [scroller setScrollEnabled:YES];    [scroller setContentSize:CGSizeMake(320, 420)];        [superviewDidLoad];}



Using setcontentoffset:animated:

Objective C Code?
1234 UIScrollView*scrollView = ...;CGPoint newOffset = scrollView.contentOffset;newOffset.y = 0;[scrollView setContentOffset:newOffset animated:YES];




If you need the effect of an opening animation, the Viewcontroller implementation in ScrollView

Objective C Code?
1234567891011121314151617 - (void)viewDidLoad{    [superviewDidLoad];    // ...    CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);    [self.scrollView setContentOffset:bottomOffset animated:NO];}- (void)viewDidAppear:(BOOL)animated{    [superviewDidAppear:animated];    CGPoint newOffset = self.scrollView.contentOffset;    newOffset.y = 0;    [self.scrollView setContentOffset:newOffset animated:YES];}




Move slowly, using the timer to achieve:

Objective C Code?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 - (void)viewDidLoad{    [superviewDidLoad];    // ...    CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);    [self.scrollView setContentOffset:bottomOffset animated:NO];}- (void)viewDidAppear:(BOOL)animated{        [superviewDidAppear:animated];    CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);    //设置延迟时间    floatscrollDurationInSeconds = 4.0;    //计算timer间隔    floattotalScrollAmount = bottomOffset.y;    floattimerInterval = scrollDurationInSeconds / totalScrollAmount;    [NSTimerscheduledTimerWithTimeInterval:timerInterval target:selfselector:@selector(scrollScrollView:) userInfo:nilrepeats:YES];}- (void)scrollScrollView:(NSTimer*)timer{    CGPoint newScrollViewContentOffset = self.scrollView.contentOffset;    //向上移动 1px    newScrollViewContentOffset.y -= 1;    newScrollViewContentOffset.y = MAX(0, newScrollViewContentOffset.y);    //如果到顶了,timer中止    if(newScrollViewContentOffset.y == 0) {        [timer invalidate];    }    //最后设置scollView‘s contentOffset    self.scrollView.contentOffset = newScrollViewContentOffset;}

Implementation of ScrollView Auto-scrolling in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.