如何将框架从 UIScrollView 转换为 UIView
How to convert frame from UIScrollView to UIView
我想将框架从 UIScrollView 转换为 UIView,但不是很精确。
这是我的代码:
//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];
如果放大缩小scrollView,moveView在scrollView放大缩小时按比例改变位置
- (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;
}
当 scrollView 放大缩小时,我无法准确地更改位置 moveView。请帮我解决这个问题。
首先我要说的是,您真的需要为所有这些东西使用自动版式。或者 none 如果您旋转设备或在不同的设备上等,这将起作用。仅使用框架来定位视图不再是行之有效的方法。它会在一定程度上起作用。是的,自动布局是一个学习曲线,但您只需要了解它并一直使用它。
但是查看代码有两个问题很突出:在 didEndZooming 方法的开始,您设置了两次比例,我不确定为什么 -
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
稍后,您以不同的方式两次设置 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;
设置 x 和 y 值与将原点设置为 scrollView.contentOffset 相同 - 因此首先将原点设置为内容偏移位置,然后设置为 0,0 - 这没有意义。解决这些问题可能会解决问题。
我想将框架从 UIScrollView 转换为 UIView,但不是很精确。 这是我的代码:
//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];
如果放大缩小scrollView,moveView在scrollView放大缩小时按比例改变位置
- (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;
}
当 scrollView 放大缩小时,我无法准确地更改位置 moveView。请帮我解决这个问题。
首先我要说的是,您真的需要为所有这些东西使用自动版式。或者 none 如果您旋转设备或在不同的设备上等,这将起作用。仅使用框架来定位视图不再是行之有效的方法。它会在一定程度上起作用。是的,自动布局是一个学习曲线,但您只需要了解它并一直使用它。
但是查看代码有两个问题很突出:在 didEndZooming 方法的开始,您设置了两次比例,我不确定为什么 -
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
稍后,您以不同的方式两次设置 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;
设置 x 和 y 值与将原点设置为 scrollView.contentOffset 相同 - 因此首先将原点设置为内容偏移位置,然后设置为 0,0 - 这没有意义。解决这些问题可能会解决问题。