2

我有一个带有自定义单元格的 UITableView。

我想在每个单元格中实现这种 UIScrollView。

每个滚动视图中我可以拥有的最大图像数量是 5。它可以更少,但不能更多。

在此处输入图像描述

我有图形和一切。我想用 UIPageControl 来实现它,因此当前图像将在 pageControl 中标记,因此我可以将其框架更改为绿光。

我现在正在使用此代码- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(12,202,295,91)];
self.scrollView.showsHorizontalScrollIndicator = NO;


NSMutableArray *imageArray = [[NSMutableArray alloc]init];

for (int i=0; i<[[[self.campArray objectAtIndex:[indexPath row]]valueForKey:@"images"]count]; i++) {


    UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(i*130, 0, 108, 92)];

    [imageView setImage:[UIImage imageNamed:@"Frame-03.png"]];

    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 96, 81)];
    [imgV setImageWithURL:[NSURL URLWithString:[[[[self.campArray objectAtIndex:[indexPath row]]valueForKey:@"images"]objectAtIndex:i]valueForKey:@"thumbnailURL"]]];

    imgV.contentMode=UIViewContentModeScaleToFill;

    imgV.tag=i+1;

    [imageView  addSubview:imgV];

    [imageArray addObject:imageView];

    [imageView release];
    [imgV release];

}



NSInteger numberOfViews = imageArray.count
;
for (int i = 0; i < numberOfViews; i++) {

    [self.scrollView addSubview:[imageArray objectAtIndex:i]];
}

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomImage)];
[self.scrollView addGestureRecognizer:singleTap];   


self.scrollView.tag = [indexPath row];

UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 108, 81)];

pgCtr.numberOfPages=numberOfViews;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[cell.contentView addSubview:pgCtr];

//set the scroll view content size
self.scrollView.contentSize = CGSizeMake(130 * 
                                         numberOfViews, 
                                         self.scrollView.frame.size.height);
//add the scrollview to this view
[cell.contentView addSubview:self.scrollView];
return cell;

谢谢!

4

1 回答 1

0

我会根据需要在滚动视图中UIScrollView设置你的对象来做到这一点。UIImageView

当用户滚动图像并放手时,您会希望最靠近中心的图像居中并成为突出显示的图像。我们需要自己处理。所以创建一个UIScrollViewDelegate并实现该方法scrollViewWillBeginDecelerating:

我们的下一个问题是弄清楚如何使滚动缓慢到以最近的图像为中心的停止。这有点棘手,但很有可能。我们需要知道一些事情。就像我们的滚动视图的可见区域实际上只是更大图片上的一个移动窗口。我们需要知道这个窗口现在在哪里,我们需要知道所有项目在大图中的位置。

取滚动视图的宽度(不是内容,滚动视图本身)并除以 2。你有中心,但我们需要将它与内容的当前位置进行比较。将其与您放入滚动视图中的每个属性contentOffset进行比较。使用和UIScrollViewframeUIImageVieworigin.xsize.width

CGFloat center = self.scrollView.frame.size.width/2 + self.scrollView.contentOffset.x;
for (UIImageView *view in scrollViewImages) {
    CGFloat leftEdge = view.frame.origin.x;
    CGFloat rightEdge = leftEdge + view.frame.size.width;

    // I didn't account the user stopping in a gap between images
    if (center >= leftEdge && center <= rightEdge) {
        [self.scrollView setContentOffset:leftEdge+view.frame.size.width/2 animated:YES];
        self.selectedImage = view;
        break;
    }
}

然后只需更新图像,UIPageControl以及您需要做的任何事情。

于 2012-12-21T17:36:36.283 回答