简体   繁体   中英

How to convert frame from UIScrollView to UIView

I want to convert frame from UIScrollView to UIView, but not exaclty. This is my code:

    //Create UIScrollView addSubview self.view
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [scrollView setBouncesZoom:YES];
    [scrollView setMinimumZoomScale:1];
    [scrollView setZoomScale:4];
    scrollView.delegate = self;

    //create UIView overlay UIScrollView and addSubview self.view
    overlayView = [[UIView alloc] initWithFrame:self.view.bounds];
    overlayView.backgroundColor = [UIColor blueColor];
    overlayView.alpha = 0.5;
    [self.view addSubview:overlayView];

    //create UIView addSubView overlayView, can move change postion
    UIView *moveView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    moveView.backgroundColor = [UIColor redColor];
    [overlayView addSubView:moveView];

If zoom in, zoom out scrollView, moveView change position by ratio when scrollView zoom in zoom out.

    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    [scrollView setZoomScale:scale+0.01 animated:NO];
    [scrollView setZoomScale:scale animated:NO];

    CGRect visibleRect;    
    visibleRect.origin = scrollView.contentOffset;
    visibleRect.size = CGSizeMake(scrollView.bounds.size.width * scrollView.zoomScale, scrollView.bounds.size.height * scrollView.zoomScale);
    visibleRect.origin.x = 0;
    visibleRect.origin.y = 0;

    overlay.frame = visibleRect;
    CGRect moveRect = moveView.frame;
    moveRect.origin.x *= scrollView.zoomScale;
    moveRect.origin.y *= scrollView.zoomScale;
    moveRect.size.width *= scrollView.zoomScale;
    moveRect.size.height *= scrollView.zoomScale;
    moveView.frame = moveRect;
}

I cannot change postion moveView exactly when scrollView zoom in zoom out. Please help me resolve this issue.

First I'd say, you really need to be using autolayout for all this stuff. Or none of this will work if you rotate the device, or on different devices, etc. Just using frames to position views is just not the done way anymore. It will work to a degree though. Yes autolayout is a learning curve but you just need to know it and use it all the time.

But looking at the code there two issues stand out : at the start of the didEndZooming method, you set the scale twice, Im not sure why -

[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];

Then also later, you set the origin of the visibleRect property twice in different ways :

visibleRect.origin = scrollView.contentOffset;
visibleRect.size = CGSizeMake(scrollView.bounds.size.width * scrollView.zoomScale, scrollView.bounds.size.height * scrollView.zoomScale);
visibleRect.origin.x = 0;
visibleRect.origin.y = 0;

Setting the x and y values there is the same as setting the origin to the scrollView.contentOffset - so first you set the origin to the content offset position, then to 0,0 - which doesnt make sense. Clearing up those problems might sort things out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM