【问题标题】:How to keep UIView center using UIScrollview contentoffset iOS?如何使用 UIScrollview contentoffset iOS 保持 UIView 中心?
【发布时间】:2019-08-31 20:22:51
【问题描述】:

我在 UIScrollView 内有 UIView,其中包含 4 个子视图。在点击子视图时,我想调整滚动视图的contentoffset 属性,使我点击的subview 与屏幕中心对齐。

我尝试了一些逻辑,但它无法正常工作。

-(void)onDragViewTapped:(UITapGestureRecognizer *)recognizer
{
    CGFloat width = self.scrollVw.contentSize.width/2;
    CGFloat vwWidth = recognizer.view.frame.size.width/2;
    width = width - vwWidth - vwWidth/self.scrollVw.zoomScale - 
    width/self.scrollVw.zoomScale;

    CGFloat height = self.scrollVw.contentSize.height/2;
    CGFloat vwHeigh = recognizer.view.frame.size.height/2;
    height = height- vwHeigh -  vwHeigh/self.scrollVw.zoomScale - 
    height/self.scrollVw.zoomScale;

    [self.scrollVw setContentOffset:CGPointMake(width, height) 
    animated:YES];
}

【问题讨论】:

    标签: ios objective-c uiscrollview


    【解决方案1】:

    这对我有用(实际上没有缩放):

    // This view below here is the view of the view controller, 
    // use the container view instead to align the center to
    let newX = tappedView.center.x - view.center.x
    let newY = tappedView.center.y - view.center.y
    scrollview.setContentOffset(CGPoint(x: newX, y: newY), animated: true)
    

    【讨论】:

    • 是的,无需缩放即可完美运行。但是我们如何通过缩放比例来升级呢?
    【解决方案2】:

    根据 Apple 文档,在视图坐标系之间进行转换

    将一个点从接收器的坐标系转换为指定视图的坐标系。

    • func convert(CGPoint, to: UIView?) -> CGPoint

    将一个点从给定视图的坐标系转换为接收器的坐标系。

    • func convert(CGPoint, from: UIView?) -> CGPoint

    将一个矩形从接收者的坐标系转换到另一个坐标系 查看。

    • func convert(CGRect, to: UIView?) -> CGRect

    将矩形从另一个视图的坐标系转换为接收者的坐标系。

    • func convert(CGRect, from: UIView?) -> CGRect

    我认为这会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      相关资源
      最近更新 更多