OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 中缩放 Scrollview 图像? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:47
标题: ios - 如何在 iOS 中缩放 Scrollview 图像?

我是 iOS 开发中的新手。我制作了一个 ScrollView,它包含我的 JSOn 数组图像,并且我的 Scrollview 启用了分页,我添加了一个 Scrollview 的方法来缩放 viewForZoomingInScrollView 然后它缩放我的第一个图像并重叠在第二个图像上当 viewForZoomingInScrollView 方法在这里调用我的 Scrollview 代码和 ViewForZoomingInScrollView 方法时,只想缩放我选择的图像

Scrollview添加图片的代码

for( index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey"link"];
    bigImage=[[UIImageView alloc]init];
    bigImage.userInteractionEnabled=TRUE;
    bigImage.image=nil;
    bigImage.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    bigImage.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    [bigImage setMultipleTouchEnabled:YES];
    [bigImage removeFromSuperview];
    [bigImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed"1.png"]];
    [bigImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:bigImage atIndex:index];
    [self.zoomScroll addSubview:bigImage];
    self.zoomScroll.clipsToBounds=YES;
    [self.zoomScroll addSubview:[self.objectarray objectAtIndex:index]];
}
[self.zoomScroll addSubview:bigImage];

这里 self.imagesa 是 Json Parsed 数组,self.imagearray 是 NSMuttable 数组。

及缩放方法

-(UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView
{
for (index=0 ; index <[self.imageArray count]; index ++)
{
    return [self.zoomScroll.subviews objectAtIndex:index];
}
return nil;
}

但它不能正常工作,它仅缩放第一张图像,当缩放第一张图像并滚动我的 Scrollview 时,第二张图像重叠。在这里我还为 Scrollview 设置了最大和最小比例。当我 ZoomScrollview 时,它的大小是更改并且只显示第一个图像而不是滚动。



Best Answer-推荐答案


请检查这是否对您有用,至于我的应用需求,我正在使用它。

首先,如果您将 imageViews 添加到 scrollView,然后在将每个 imageView 添加到 scrollView 之前设置每个 imageView 的 tag 属性的值,如果您为要添加的所有 imageViews 的 tag 属性设置相同的值就可以了 ScrollView 。

然后如下添加UIScrollView的delegate方法并检查,

- (UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView 
{
    return [scrollView viewWithTag:<valueOfTagPropertySetToImageView>];
}

关于ios - 如何在 iOS 中缩放 Scrollview 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923025/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4