【问题标题】:Automatic scrolling scrollview自动滚动滚动视图
【发布时间】:2012-10-26 20:56:32
【问题描述】:

我有一个问题。我想在UIScrollView 中向用户显示一些内容。我想从下到上快速autoscrollUIScrollView(就像在苹果商店iPad)。我尝试使用DDAutoscrollview(如果有人知道的话),但它对我不起作用。有人为我提供autoscrollUIScrollView 的解决方案吗?任何代码 sn-ps 都会很好。

.h

@interface Interface1 : UIViewController {

    IBOutlet UIScrollView *scroller;
    IBOutlet UILabel *warnung;

}

@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;

.m

- (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];
}

谢谢。


> 为 TOBI 提供的 AWNSER 点赞!!!


【问题讨论】:

  • [self.scrollView setScrollsToTop:YES]怎么样;您希望以哪种方式发生这种情况?

标签: iphone objective-c ios uiscrollview


【解决方案1】:

只需使用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);
    }];
}

【讨论】:

  • 这取决于您希望滚动发生的时间:)
  • 其实我可以把代码放在viewdidappear中吗?什么意思?
  • 是否必须将高度替换为滚动视图的高度?
  • 代码假定你的视图控制器有一个属性scrollView。如果你没有,你必须在我的例子中用对你的滚动视图的有效引用替换每个self.scrollView
  • 您是否收到编译器错误,或者动画无法按您的意愿工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多