2011-06-15 84 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

相關問題