在iOS中的单个UIScrollView中缩放ImageView图像?

时间:2014-11-21 04:29:35

标签: ios iphone uiscrollview

我是iOS开发中的新手我想在UIImageView中缩放UIScrolView图片,因为我在UIImageView添加了UIView,然后我添加了UIView 1}}到UIScrollView但它没有在视图上显示任何图像。以下是我的代码

for(int index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    self.viewImages=[[UIView alloc]initWithFrame:CGRectMake(index*bigimage.frame.size.width, 0, bigimage.frame.size.width, bigimage.frame.size.height)];
    bigImage=[[UIImageView alloc]init];
    bigImage.userInteractionEnabled=TRUE;
    bigImage.tag=123;
    bigImage.tag=index;
    bigImage.userInteractionEnabled=TRUE;
    bigImage.frame=CGRectMake(index * self.viewImages.frame.size.width, 0, self.viewImages.frame.size.width, self.viewImages.frame.size.height);
    [bigImage setMultipleTouchEnabled:YES];
    [bigImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [bigImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:bigImage atIndex:index];
    [self.viewImages addSubview:bigImage];
    [self.viewImages addSubview:[self.objectarray objectAtIndex:index]];
    [self.zoomScroll addSubview:self.viewImages];
    self.zoomScroll.contentSize=self.viewImages.frame.size;

请为我提供缩放UIScrolView图片的解决方案,并将我的所有图片数组添加到我的UIScrolView。这里的大图是UIImageViewself.viewImagesUIView

1 个答案:

答案 0 :(得分:0)

您正尝试在每张图片上制作具有缩放功能的图像水平滚动条。

使用XIB文件创建名为SlideshowImageView的自定义视图,并在其上放置UIScrollView,在SlideshowImageView.h中添加以下代码

@interface SlideshowImageView : UIView <UIScrollViewDelegate>
{
    UIImageView *imageView;   
}
@property (nonatomic, strong) IBOutlet UIScrollView *zoomScrollView;
@end

将UIScrollView与属性名称zoomScrollView和scrollview委托链接起来。

现在在SlideshowImageView.m中添加以下代码

@implementation SlideshowImageView

- (void)setImageForSlide:(UIImage *)image 
{
    imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    imageView.userInteractionEnabled = TRUE;
    imageView.contentMode = UIViewContentModeCenter;
    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.zoomScrollView addSubview:imageView];
}

@end

在SlideshowImageView.m中实现以下UIScrollview委托:

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}

现在,从使用循环创建视图的控制器,您可以这样做:

for(int index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];

    //Create custom view now and add it into your main scrollview
    SlideshowImageView *iView = [[SlideshowImageView alloc] initWithFrame:CGRectMake(index*mainScrollView.frame.size.width, 0, mainScrollView.frame.size.width, mainScrollView.frame.size.height)];
    [iView setImageForSlide: [UIImage imageNamed:image]];
    [self.mainScrollView addSubview:iView];
}

如果您对实施有任何疑惑,请与我们联系。