0
我是新来的自动滚动功能。我有一个很长的.png文件。我想要做的是,当我点击一个按钮时,我想自动开始滚动该图像。我已经通过sdk中的scrollView示例代码。但我感到困惑。请任何一个帮助我提前如何将自动滚动功能添加到iphone sdk中的视图?
感谢 Praveena
我是新来的自动滚动功能。我有一个很长的.png文件。我想要做的是,当我点击一个按钮时,我想自动开始滚动该图像。我已经通过sdk中的scrollView示例代码。但我感到困惑。请任何一个帮助我提前如何将自动滚动功能添加到iphone sdk中的视图?
感谢 Praveena
滚动通过设置内容偏移来实现的。
想象构建这样的一个视图控制器(在-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];
要使滚动是连续的,您需要设置一个更新内容偏移的计时器。
- (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];
}
感谢您发表的答案!虽然代码片段可以回答这个问题,但添加一些附加信息仍然很棒,比如解释等。 – j0k 2012-09-27 18:58:23