2012-01-02 43 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