2011-06-15 81 views

回答

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

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

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]; 
} 
+0

感谢您发表的答案!虽然代码片段可以回答这个问题,但添加一些附加信息仍然很棒,比如解释等。 – j0k 2012-09-27 18:58:23

相关问题