【问题标题】:Zoom an ImageView Image in Single UIScrollView in iOS?在 iOS 的单个 UIScrollView 中缩放 ImageView 图像?
【发布时间】:2014-11-21 04:29:35
【问题描述】:

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

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.viewImages 是我的UIView

【问题讨论】:

    标签: ios iphone uiscrollview


    【解决方案1】:

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

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

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

    将 UIScrollView 与属性名称 zoomScrollView 和滚动视图委托链接起来。

    现在在 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];
    }
    

    如果您对实施有任何困惑,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 2020-06-18
      • 2018-07-19
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多