我有一个问题。我想在UIScrollView
中向用户显示一些内容。我希望autoscroll
UIScrollView
从下到上快速(例如在apple store iPad
中)。我尝试使用DDAutoscrollview
(如果有人知道),但它对我不起作用。 autoscroll
UIScrollView
让某人成为我的解决方案吗?任何代码片段都会很好。
@interface Interface1 : UIViewController {
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *warnung;
}
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
- (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)];
[super viewDidLoad];
}
感谢。
答案 0 :(得分:7)
只需使用setContentOffset:animated:
UIScrollView *scrollView = ...;
CGPoint newOffset = scrollView.contentOffset;
newOffset.y = 0;
[scrollView setContentOffset:newOffset animated:YES];
修改强>
要像使用某种开始动画一样使用它,你可以在scrollView的视图控制器中执行此操作:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
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
{
[super viewDidAppear:animated];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
编辑2/3:
要使滚动更慢,请使用:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
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
{
[super viewDidAppear:animated];
float scrollDuration = 4.0;
[UIView animateWithDuration:scrollDuration animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}];
}