2012-01-02 56 views
2

_分頁,滾動型

viewdidload 
{ 
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; 
    for (int i = 0; i < colors.count; i++) { 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     subview.backgroundColor = [colors objectAtIndex:i]; 
     [self.scrollView addSubview:subview]; 
     [subview release]; 
    } 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height); 

    self.pageControl.currentPage = 0; 
    self.pageControl.numberOfPages = colors.count; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    if (!pageControlBeingUsed) { 
     // Switch the indicator when more than 50% of the previous/next page is visible 
     CGFloat pageWidth = self.scrollView.frame.size.width; 
     int page = floor((self.scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
     self.pageControl.currentPage = page; 
    } 
} 

我使用這個代碼用不同的顏色顯示滾動視圖分頁,鑑定,我只是想用圖片來代替colors.i來代替它正在使用此代碼

NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"[email protected]"], [UIImage imageNamed:@"[email protected]"], [UIImage imageNamed:@"[email protected]"], nil]; 
    for (int i = 0; i < colors.count; i++) { 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 

     UIImageView*subview = [[UIView alloc] initWithFrame:frame]; 
     UIImage *imggg = [colors objectAtIndex:i]; 
     [subview setBackgroundColor:[UIColor colorWithPatternImage:imggg]]; 

     [self.scrollView addSubview:subview]; 
     [subview release]; 
    } 

但我只得到兩個視圖,圖像不是大小,以適應它被取消。如何設置圖像與上述code.Please幫我解決這個問題。 在此先感謝。

回答

0
UIView *subview = [[[UIView alloc] initWithFrame:frame] autorelease]; 
[subview setBackgroundColor:[colors objectAtIndex:i]]; 

[[self scrollView] addSubview:subview]; 

這看起來像你的代碼是試圖做...

如果你想每個頁面,雖然上的圖像:

UIImage *img = [UIImage imageNamed:@"nameOfImageFile"]; 
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease]; 

[[self scrollView] addSubview:imageView]; 

有多個圖像的每一頁,只需使用該頁面的一組UIImage。

NSArray *imageNames = [NSArray arrayWithObjects:@"image1Name", @"image2name", @"image3Name", nil]; 

UIImage *img = [UIImage imageNamed:[imageNames objectAtIndex:pageNumber]]; 
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease]; 

[[self scrollView] addSubview:imageView]; 
+0

但在上面的代碼中,我只得到@「nameOfImageFile」一個圖像,但我有多個圖像在每個頁面上。 – stackiphone 2012-01-02 13:18:30

+0

請參閱添加多個圖像的實現。 – ColdLogic 2012-01-03 15:04:45

1

我得到了答案,通過減少數組中的圖像的大小正是滾動視圖的大小。我的滾動視圖大小爲320,424,我將圖像的大小切分爲320,424。它完美的作品。謝謝。

0
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    IBOutlet UIScrollView *scrollView; 
} 

@end 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"chiranjeevi.jpeg"], [UIImage imageNamed:@"AR.jpeg"], [UIImage imageNamed:@"sachin.jpeg"], nil]; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*colors.count,scrollView.frame.size.height); 
    for (int i = 0; i <[colors count]; i++) 
    { 
     UIImage *imggg = [colors objectAtIndex:i]; 
     CGRect frame; 
     frame.origin.x =scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = scrollView.frame.size;  

     UIImageView *subview = [[UIImageView alloc] initWithFrame:frame]; 
     subview.image=imggg; 

     [scrollView addSubview:subview]; 

    } 

} 
- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end