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(nonatomic, retain) IBOutletUIScrollView* 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