如何在iOS中缩放Scrollview图像?

时间:2014-11-14 04:42:30

标签: ios objective-c iphone uiscrollview

我是iOS开发中的newBie.i使ScrollView包含我的JSOn数组图像和我的Scrollview已启用Paging并且我为Scrollview添加了一个方法来缩放viewForZoomingInScrollView然后它是缩放我的第一个图像并在第二个图像上重叠我想要在viewForZoomingInScrollView方法时只缩放我选择的图像这里调用我的Scrollview代码和forForForoToomingInScrollView方法是

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 *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
for (index=0 ; index <[self.imageArray count]; index ++)
{
    return [self.zoomScroll.subviews objectAtIndex:index];
}
return nil;
}

但它不是正常工作它只是缩放第一张图像和缩放第一张图像和我滚动我的滚动视图然后第二张图像被覆盖。在这里我还设置了Scrollview的最大和最小比例。当我缩放时,它的尺寸是变化的,只显示第一张图像而不是滚动。

4 个答案:

答案 0 :(得分:2)

请检查这是否对您有用,至于我的应用程序要求,我正在使用此功能。

首先,如果要将imageView添加到scrollView中,然后在将每个imageView添加到scrollView之前设置每个imageView的tag属性的值,如果为要添加的所有imageViews的tag属性设置相同的值,则可以。滚动视图。

然后按如下方式添加UIScrollView的委托方法并检查,

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

答案 1 :(得分:0)

刚刚使用了以下方法: -

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

 {

   return self.imageView;

 }
- (void)viewDidLoad {

    [super viewDidLoad];

     self.scrollView.minimumZoomScale=0.5;

     self.scrollView.maximumZoomScale=6.0;

     self.scrollView.contentSize=CGSizeMake(1280, 960);

     self.scrollView.delegate=self;

    }

另一种缩放方法: -

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view   atScale:(float)scale
   {}

答案 2 :(得分:0)

要实现您的要求,您必须对当前结构进行一些更改。

目前,您要将所有ImageViews添加到单个ScrollView,而不是必须为每个ScrollViews创建ImageView并添加所有ScrollViews单身ScrollView

您的代码应如下所示:

UIScrollView *parentScrollView = [[UIScrollView all] initWithFrame:<frame>];

for( index=0; index < [self.imagesa count]; index++)
{
    UIScrollView *innerScrollView = [UIScrollView alloc] initWithFrame:CGRectMake(index * parentScrollView.frame.size.width, 0, parentScrollView.frame.size.width, parentScrollView.frame.size.height)];

    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    bigImage=[[UIImageView alloc]init];
    bigImage.tag = 123; // Helps to get ImageView to given as zoom view in scrollview delegate
    bigImage.userInteractionEnabled=TRUE;
    bigImage.image=nil;
    bigImage.frame=CGRectMake(0, 0, innerScrollView.frame.size.width, innerScrollView.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];

    [parentScrollView addSubview:innerScrollView]; 
}

您的缩放方法可能看起来像

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    UIImageView *imageView = [scrollView viewWithTag:123]; // returns ImageView
    if(imageView){
        return imageView;
    }
    return nil;
}

答案 3 :(得分:0)

从此链接检查我的回答,

Check the link

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return self.imageView;

}
- (void)viewDidLoad {

    [super viewDidLoad];

    self.scrollView.minimumZoomScale=0.5;

    self.scrollView.maximumZoomScale=6.0;

    self.scrollView.contentSize=CGSizeMake(1280, 960);

    self.scrollView.delegate=self;

}

Apple自身的解决方案,Apple Documentation link