我想使我的uitextview
在应用程序启动时自动滚动。任何人都可以帮我一个详细的代码?我是iPhone SDK的新手。使uitextview以编程方式滚动
回答
.h文件中
@interface Credits : UIViewController
{
NSTimer *scrollingTimer;
IBOutlet UITextView *textView;
}
@property (nonatomic , retain) IBOutlet UITextView *textView;
- (IBAction) buttonClicked ;
- (void) autoscrollTimerFired;
@end
.m文件
- (void) viewDidLoad
{
// it prints the initial position of text view
NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height);
if (scrollingTimer == nil)
{
// A timer that updates the content off set after some time so it can scroll
// you can change time interval according to your need (0.06)
// autoscrollTimerFired is the method that will be called after specified time interval. This method will change the content off set of text view
scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];
}
}
- (void) autoscrollTimerFired
{
CGPoint scrollPoint = self.textView.contentOffset; // initial and after update
NSLog(@"%.2f %.2f",scrollPoint.x,scrollPoint.y);
if (scrollPoint.y == 583) // to stop at specific position
{
[scrollingTimer invalidate];
scrollingTimer = nil;
}
scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); // makes scroll
[self.textView setContentOffset:scrollPoint animated:NO];
NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height);
}
希望它可以帮助你....
UITextView派生自UIScrollview,因此您可以使用-setContentOffset:animated:设置滚动位置。
假设你想以每秒10点的速度顺利滚动,你会做类似的事情。
- (void) scrollStepAnimated:(NSTimer *)timer {
CGFloat scrollingSpeed = 10.0; // 10 points per second
NSTimeInterval repeatInterval = [timer timeInterval]; // ideally, something like 1/30 or 1/10 for a smooth animation
CGPoint newContentOffset = CGPointMake(self.textView.contentOffset.x, self.textView.contentOffset.y + scrollingSpeed * repeatInterval);
[self.textView setContentOffset:newContentOffset animated:YES];
}
当然,您必须设置计时器,并确保在视图消失时取消滚动等等。
你必须声明NSTimer为实例变量,并在-viewWillAppearAnimated:中设置一个好看的重复间隔。然后确保无效并在-dealloc和-viewWillDisappearAnimated:中释放它。 –
我不能给你完整的代码,因为我不确定你了解代码在做什么。一定要尝试理解该代码背后的逻辑。它正在做的只是滚动到底部给定一个特定的速度。 –
在你的.h文件中,你必须做这样的事情,你声明你的其他实例变量:NSTimer * scrollingTimer; @Maulik向您展示了如何设置和使计时器无效。 –
- 1. 以编程方式滚动UITextView
- 2. 以编程方式禁用水平滚动到UITextView
- 3. iOS4的:无法以编程方式滚动到的UITextView
- 4. 以编程方式创建3个UITextView
- 5. Swift - 以编程方式定位UITextView
- 6. 以编程方式粘贴到iPhone UITextView
- 7. 以编程方式平滑滚动UIPageViewController
- 8. 以编程方式滚动THorzScrollBox Firemonkey XE6
- 9. 以编程方式滚动视图
- 10. scrollViewDidScroll:以编程方式滚动时?
- 11. 以编程方式滚动EditText
- 12. 以编程方式滚动ListView
- 13. 试图以编程方式滚动scrollviewer
- 14. 如何以编程方式滚动SurfaceScatterView
- 15. 以编程方式水平滚动PDFView
- 16. 如何以编程方式滚动UIScrollVIew
- 17. 以编程方式滚动TableLayout
- 18. 以编程方式滚动的ViewPager
- 19. 如何以编程方式滚动ScrollView?
- 20. 以编程方式滚动QScrollArea
- 21. 以编程方式滚动PreferenceActivity
- 22. UIScrollView以编程方式滚动
- 23. 以编程方式禁用ScollViewer滚动
- 24. 如何以编程方式滚动TextMergeViewer?
- 25. 以编程方式滚动JMapViewer地图
- 26. 以编程方式滚动视图
- 27. 以编程方式滚动SWT表
- 28. 以编程方式滚动UIScrollView
- 29. 以编程方式滚动UIScrollView
- 30. 如何以编程方式滚动UIScrollView?
究竟如何你想让它滚动?你想让它滚动到最后,还是中间的一个点?或者你想让它从上到下缓慢滚动? – mahboudz
http://stackoverflow.com/questions/1088960/iphone-auto-scroll-uitextview-but-allow-manual-scrolling-also可能的副本 – tipycalFlow
我想从上到下滚动uitextview,慢慢地。并且没有允许textview交互。我的意思是,用户不能在textview中编辑任何东西。 – user919050