自动滚动滚动视图(Automatic scrolling scrollview)

2019-08-02 09:31发布

我有个问题。 我想显示用户在某些内容UIScrollView 。 我想autoscrollUIScrollView从底部快速顶部(像苹果商店iPad )。 我试图用DDAutoscrollview (如果有人知道),但它并没有为我工作。 难道有人给我一个解决方案, autoscroll一个UIScrollView ? 任何代码段将是很好的。

。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!


Answer 1:

只需使用setContentOffset:animated:

UIScrollView *scrollView = ...;
CGPoint newOffset = scrollView.contentOffset;
newOffset.y = 0;
[scrollView setContentOffset:newOffset animated:YES];

编辑:

要使用它像某种开始动画,你可以在滚动视图的视图控制器做到这一点:

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


文章来源: Automatic scrolling scrollview