自动滚动scrollview

时间:2012-11-06 20:34:21

标签: iphone objective-c ios uiscrollview

我有一个问题。我想在UIScrollView中向用户显示一些内容。我希望autoscroll UIScrollView从下到上快速(例如在apple store 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而言,这已经成功了!!!


1 个答案:

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