1

スクロール ビューでページングを実装しています。サイズ (259,263) のスクロール ビューとサイズ (741, 723) の画像。これまでのところ、私は少し成功することができます。画像が大きすぎて重なっています。画像ビューで画像サイズを調整できません。これが私のコードです:

- (void)setupPage
{
 NSMutableArray *imageArray= [[NSMutableArray
alloc]initWithObjects:@"female.png",@"male.png",nil];

int nimages;

CGFloat cx = 0;
for (nimages = 0; nimages<[imageArray count]; nimages++)
{ 
    NSString *imageName = [NSString stringWithFormat:@"%@",[imageArray objectAtIndex:nimages]];
    UIImage *image = [UIImage imageNamed:imageName];
    if (image == nil)
            {
        break;
    }

        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    CGRect rect = imageView.frame;
    rect.size.height = image.size.height;
    rect.size.width = image.size.width;
    rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
    rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);

    imageView.frame = rect;

    [scrollView addSubview:imageView];
    [imageView release];

    cx += scrollView.frame.size.width;
}

self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
 }

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage)
{
    return;
}
CGFloat pageWidth = _scrollView.frame.size.width/2;
int page = floor((_scrollView.contentOffset.x - pageWidth/2 ) / pageWidth) + 1;
pageControl.currentPage = page;
} 

画像ビューに静的サイズを与えることで解決策を取得します

これが私のコードです NSUInteger nimages;

for (nimages = 1; nimages <= [imageArray count]; nimages++)
{

    NSString *imageName = [NSString stringWithFormat:@"%@",[imageArray 
   objectAtIndex:nimages-1]];
    UIImage *image = [UIImage imageNamed:imageName];
    if (image == nil)
    {
        break;
    }


    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;

       imageView.tag = nimages;   
    [scrollView addSubview:imageView];
    [imageView release];

}

UIImageView *view = nil;
NSArray *subviews = [scrollView subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

// set the content size so it can be scrollable
[scrollView setContentSize:CGSizeMake(([imageArray count] * kScrollObjWidth), [scrollView 
  bounds].size.height)];
4

0 に答える 0