如何在iphone sdk中为视图添加自动滚动功能?

时间:2011-06-15 07:22:02

标签: objective-c ios4

我是自动滚动功能的新手。我有一个非常冗长的.png文件。我想要做的是,当我点击一个按钮时,我想自动开始滚动该图像。我在sdk中浏览过scrollView示例代码。但我感到困惑。请任何人帮助我

提前致谢 Praveena

2 个答案:

答案 0 :(得分:4)

- (void) scrollView
{
    CGFloat currentOffset = scrollImage.contentOffset.x;

    CGFloat newOffset;

    if(currentOffset == 3840)
    {
        currentOffset = -320.0;

        [scrollImage setContentOffset:CGPointMake(newOffset,0.0) animated:NO];
    }

    newOffset = currentOffset + 320;

        [UIScrollView beginAnimations:nil context:NULL]; 
        [UIScrollView setAnimationDuration:2.1]; 
        [scrollImage setContentOffset:CGPointMake(newOffset,0.0)];
        [UIScrollView commitAnimations];
}

答案 1 :(得分:0)

通过设置内容偏移来实现滚动。

想象一个视图控制器构造这样的东西(例如在-viewDidLoad中):

// Load image.
UIImage *image = [UIImage imageNamed:@"image.png"];

// Create image view to hold the image.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];

[imageView setImage:image];

// Create a scroll view that is the size of the view.
scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];

// Important: If the content size is less than the scroll view frame, then it will not scroll.
[scrollView setContentSize:[image size]];

// Add to view hierarchy.
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];

要使其滚动,请执行以下操作:

[scrollView setContentOffset:CGPointMake(0, 100) animated:YES];

要使滚动连续,您需要设置一个更新内容偏移的计时器。