【问题标题】:CGAffineTransform scale moves center of view on iOS10 (and doesn't on iOS11)CGAffineTransform 比例在 iOS10 上移动视图中心(在 iOS11 上不移动)
【发布时间】:2018-01-20 00:11:52
【问题描述】:

我有一个同时支持 iOS10 和 iOS11 的应用程序。在应用程序中,我通过平移手势将 uiview 固定在手指上。平移手势仅沿y 轴移动视图中心(x 始终保持不变)。我也将 CGAffineTransform 绑定到手势,用户移动我的视图越多,它就越大。 “平移”视图包含另一个包含一些内容的视图。

现在在 iOS11 上我有这种行为(我认为这是正确的):

             > - center of content view stays on a vertical line
^  <--[      |      ]-->
|  <----[    |    ]---->
|  <------[  |  ]------>
|  <--------[ ]-------->

但在 iOS10 上我得到了这种行为:

                > - center of content view moves to the right along with scale growth 
^  <----[      /      ]>
|  <-----[    /    ]--->
|  <------[  /  ]------>
|  <--------[ ]-------->

我将内容视图添加到平移视图的代码:

// self is PannedView 
self.addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
// add constraints:
// self.width = contentView.width
// self.height = contentView.height
// self.centerX = contentView.centerX
// self.centerY = contentView.centerY

处理平移手势:

... // handle some unrelated stuff
panView.center.y = self.panStartPoint.y + panDeltaY // where panDeltaY is basically a translation of a UIPanGestureRecognizer
... // some scale calculation logic, calc scale addition depending on pan touch location
let scale = 1 + movePercent * 0.1 // where movePercent is between 0 and 1
panView.transform = CGAffineTransform.identity.scaledBy(x: scale, y: scale)

如您所见,我还将比例限制在 11.1 之间。

似乎在 iOS10 和 iOS11 上,与缩放相关的约束的工作方式不同,或者缩放以某种方式“双重”传播到子视图。

有什么想法吗?

【问题讨论】:

    标签: ios swift ios-autolayout cgaffinetransform


    【解决方案1】:

    不要对由约束定位的视图应用变换。结果是不确定的(这正是您所经历的:它“意味着”一个系统上的一件事和另一个系统上的另一件事)。

    【讨论】:

    • PannedView 不是由约束定位的,它里面的 contentView 是由约束定位的。这是否意味着如果我想对某个视图应用变换,它在整个树中的任何子视图都不能通过约束定位?
    • 自己看看。从内容视图中移除约束,看看会发生什么。
    • 已经做到了:)。谢谢。你说的对。如果我在 layoutSubviews 中手动布局 contentView 并将 translatesAutoresizingMaskIntoConstraints 设置为 true 它的行为“正确”。 ContentView 内容仍在使用约束,它似乎工作。您是否碰巧知道评论中我问题第二部分的答案:我可以保留 contentView 子视图树的约束吗?
    猜你喜欢
    • 1970-01-01
    • 2017-09-29
    • 2019-08-28
    • 2013-12-16
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多