我想将帧从UIScrollView转换为UIView,但不是exaclty。 这是我的代码:
//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,当scrollView放大缩小时,按比例移动View更改位置。
- (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放大缩小时,我无法准确更改postion moveView。请帮我解决这个问题。
答案 0 :(得分:0)
首先我要说,你真的需要在所有这些东西上使用自动布局。或者,如果你旋转设备,或者在不同的设备上,这些都不会起作用。只是使用框架定位视图不再是完成的方式。它会在一定程度上发挥作用。是autolayout是一个学习曲线,但你只需要知道它并一直使用它。
但是看看代码有两个问题突出:在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值与将origin设置为scrollView.contentOffset相同 - 所以首先将origin设置为内容偏移位置,然后设置为0,0 - 这没有意义。清理这些问题可能会解决问题。