Issue while zooming uiscrollview which is used to

2019-06-08 05:47发布

I'm developing an App which has (image saving) in one of the views, I have three different sizes which I want my images to be saved in after the user zoom and scale the image which is captured from the scroll view when the user click on save.

Below is my Codes:

After the style is selected the width and height is set depending on type chosen and then the scrollview creating method called

-(void)viewDidAppear:(BOOL)animated{
if(!([getColor length] == 0))
{
    [theColored setHidden:NO];
    [imageView setImage:image];
    [theStyle setHidden:NO];
    [theStyled setHidden:YES];

}
if(!([getStyle length] == 0))
{
    [theColored setHidden:NO];
    [imageView setImage:image];
    [theStyle setHidden:YES];
    [theStyled setHidden:NO];
    [Save setHidden:NO];


    if([theType  isEqual: @"Pant"]){
        theW = 200;
        theH = 260;
    }else if ([theType isEqual:@"Shirt"])
    {
        theW = 220;
        theH = 220;
    }else if([theType isEqual:@"Shoes"])
    {
        theW = 200;
        theH = 60;
    }
   // UIImage *savedImage = image;

    CGFloat theScale = 1.0;
    [self setTheView:image andFrameW:&theW andFrameH:&theH andTheScale:&theScale];

      }}

Below is the method:

-(void)setTheView:(UIImage *)savedImage andFrameW:(CGFloat *)theFW andFrameH:(CGFloat *)theFH andTheScale:(CGFloat *)theScale{
    NSLog(@"called");
    CGFloat theS = *theScale;
    CGFloat imgW ;
    CGFloat imgH;
    imgW = *theFW * theS;
    imgH = *theFH * theS;




    // = savedImage.size.height * theS;
    [dot removeFromSuperview];
    [scrollView setPagingEnabled:NO];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    scrollView.layer.masksToBounds=YES;
    scrollView.minimumZoomScale = 0.1f;
    scrollView.maximumZoomScale = 5.0f;
  [scrollView setZoomScale:0.5];
    scrollView.layer.borderColor = [UIColor blackColor].CGColor;
    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [scrollView setContentSize:CGSizeMake(imgW*2,imgH*2)];

    dot =[[UIImageView alloc] initWithFrame:CGRectMake(imgW/2,imgH/2,imgW,imgH)];
    dot.image=savedImage;
    dot.contentMode = UIViewContentModeScaleAspectFit;


    [scrollView setScrollEnabled:YES];
    [scrollView setTranslatesAutoresizingMaskIntoConstraints:YES];
    scrollView.frame = CGRectMake(scrollView.frame.origin.x,scrollView.frame.origin.y, *theFW , *theFH);
    [scrollView setHidden:NO];
    [scrollView setContentOffset:CGPointMake(imgW/2,imgH/2) animated:NO];
    [scrollView addSubview:dot];


}

Zooming methods:

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

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"zoomed");
    NSLog(@"%f",scale);
   [self setTheView:image andFrameW:&theW andFrameH:&theH andTheScale:&scale];

}

Problem: I have two problems here:

1- The zoom scale is not starting from last point it reached.

2- While tapping to zoom moves even before start zooming.

Update Edit

Both problems Solved

instead of calling the method again after zooming i just changed the content size

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

[self setTheView:image andFrameW:&theW andFrameH:&theH andTheScale:&scale];

}

replaced with

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

scrollView.contentSize = CGSizeMake((dot.bounds.size.width*scale)*2,(dot.bounds.size.height*scale)*2);

}

But i have new problem came up, after zooming contentOffSet is not equal up and down and also left and write

Digram explanation:

<----------------------------Content Size---------------------------->
|    left offset    |<---------Scrollviewframe--------->|Right offset|

i tried to set new content offset:

[scrollView setContentOffset:CGPointMake((dot.bounds.size.width*scale)/2,(dot.bounds.size.height*scale)/2) animated:NO];

but made nothing..

1条回答
做自己的国王
2楼-- · 2019-06-08 06:40

Problem solved i wanted to share the answer if someone wants to use scrollview to crop and zoom while allowsEditing = YES; doesn't gives the ability to set the rect size

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

CGFloat zoomedW = dot.bounds.size.width*scale;
CGFloat zoomedH = dot.bounds.size.height*scale;
CGFloat frameW = scrollView.frame.size.width;
CGFloat frameH = scrollView.frame.size.height;

scrollView.contentSize = CGSizeMake(frameW+zoomedW ,frameH+zoomedH);

}
查看更多
登录 后发表回答