0

我试图在 UIScrollview 中创建 UIimages 列表。当我水平实现它时,它会按照我想要的方式工作:

在此处输入图像描述

如您所见,图像粘在一起。

然后我垂直尝试了它。它可以工作,但图像之间出现不需要的空间,如下所示:

在此处输入图像描述

这是我的代码:

@synthesize scrollView, pageControl;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    pageControlBeingUsed = NO;

    NSArray *pageImages = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"photo1.png"],
                               [UIImage imageNamed:@"photo2.png"],
                               [UIImage imageNamed:@"photo3.png"],
                               [UIImage imageNamed:@"photo4.png"],
                               [UIImage imageNamed:@"photo5.png"],
                               nil];

    for (int i = 0; i < pageImages.count; i++) {
        CGRect frame;
        //frame.origin.x = self.scrollView.frame.size.width * i;
        //frame.origin.y = 0;

        frame.origin.x = 0;
        frame.origin.y = self.scrollView.frame.size.height * i;

        frame.size = self.scrollView.frame.size;

        UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
        newPageView.contentMode = UIViewContentModeScaleAspectFit;
        newPageView.frame = frame;
        [self.scrollView addSubview:newPageView];
    }

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

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

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageImages.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.height;
        int page = floor((self.scrollView.contentOffset.y - pageWidth / 2) / pageWidth) + 1;

        //CGFloat pageWidth = self.scrollView.frame.size.width;
        //int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

        self.pageControl.currentPage = page;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (IBAction)changePage {
    // Update the scroll view to the appropriate page
    CGRect frame;
    frame.origin.x = 0;
    frame.origin.y = self.scrollView.frame.size.height * self.pageControl.currentPage;

//    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
//  frame.origin.y = 0;


    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];

    // Keep track of when scrolls happen in response to the page control
    // value changing. If we don't do this, a noticeable "flashing" occurs
    // as the the scroll delegate will temporarily switch back the page
    // number.
    pageControlBeingUsed = YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.scrollView = nil;
    self.pageControl = nil;
}

- (void)dealloc {
    [scrollView release];
    [pageControl release];
    [super dealloc];
}

@end

在代码中,水平代码行被注释。

你能帮我删除垂直空间中不需要的空间吗?

4

2 回答 2

1

在这里,图像大小产生了问题。检查这个newPageView.contentMode = UIViewContentModeScaleAspectFill;而不是newPageView.contentMode = UIViewContentModeScaleAspectFit;

于 2012-09-22T10:47:08.940 回答
1

编辑这部分代码

UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
        newPageView.contentMode = UIViewContentModeScaleAspectFit; // this statement is making your newPageView bigger then the image, hence spaces are coming when scrolling. !!
        newPageView.frame = frame;
        [self.scrollView addSubview:newPageView];

动态创建您的视图“ newPageView”,以便视图与您的图像具有精确的大小。

于 2012-09-22T10:48:58.687 回答