滚动单击UIScrollView中的屏幕中心



我有带有标签的UIScrollView。我正在获得触摸事件坐标。当用户点击屏幕时,我将点击坐标存储在变量中,然后我想以这样的方式滚动视图(使用类似 [myScrollView setContentOffset] 的东西),这样点击点的 y 坐标将成为我的视图的中心,所以当你点击一些文本时,它会自动滚动到屏幕中心。我不确定如何计算出这个的数学...

任何帮助都非常感谢!

UIScrollView *aScrollView;      
CGPoint touchPoint;
[aScrollView scrollRectToVisible:CGRectMake(touchPoint.x - roundf(aScrollView.frame.size.width/2.),
                                                  touchPoint.y - roundf(aScrollView.frame.size.height/2.), 
                                                  aScrollView.frame.size.width, 
                                                  aScrollView.frame.size.height)  
                                                  animated:YES];

应该工作。

我的解决方案非常相似,但我使用的是比例,我想居中查看,而不是触摸点。所以我需要先计算中心点。

我的最终代码:

UIView *view; //View to center
UIScrollView *scrollView; //scroll view
CGPoint point = CGPointMake(view.frame.origin.x + view.frame.size.width / 2, 
                            view.frame.origin.y + view.frame.size.height / 2);
CGRect rectToZoom = CGRectMake(point.x * scrollView.zoomScale - roundf(scrollView.frame.size.width /2.),
                               point.y * scrollView.zoomScale - roundf(scrollView.frame.size.height /2.),
                               scrollView.frame.size.width,
                               scrollView.frame.size.height);
[scrollView scrollRectToVisible:rectToZoom animated:YES];

最新更新