在scrollview中的分页

时间:2012-01-02 12:20:19

标签: iphone

_

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

我使用此代码在不同颜色的scrollview中显示分页,用于识别,我只想用图像而不是colors替换它。我正在使用此代码

NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"h1@2x"], [UIImage imageNamed:@"h2@2x"], [UIImage imageNamed:@"h2.1@2x"], 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];
    }

但是我只得到了两个视图,并且图像的大小不适合它被定位。如何用上面的代码设置图像。请帮我解决这个问题。 提前谢谢。

3 个答案:

答案 0 :(得分:1)

我通过将数组中的图像大小减小到滚动视图的大小来得到答案。我的滚动视图大小为320,424,我将图像的大小切成320,424。它完美地运作。感谢。

答案 1 :(得分: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];

每个页面都有多个图像,只需使用一个UIImages数组即可。

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

答案 2 :(得分: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