ios - 将 CGAffineTransform Scale 应用于 UIView 使图层边框也变大
<p><p>我有一个 UIView,里面有一个 UIImageView。我在 UIVIew 中添加了一个 UIPinchGestureRecognizer 来处理捏合和缩放,并使 UIView 与 UIImageView 一起增长。</p>
<p>我的 UIView 有一个边框。我以这种方式添加了边框:</p>
<pre><code>self.layer.borderColor = .CGColor;
self.layer.borderWidth = 1.0f;
self.layer.cornerRadius = 8.0f;
</code></pre>
<p>而我遇到的问题是,我找不到在保持边框宽度相同的同时使 UIView 更大的方法。捏合和缩放时,边框会变粗。 </p>
<p>这是我的 UIPinchGestureRecognizer 处理程序:</p>
<pre><code>- (void)scale:(UIPanGestureRecognizer *)sender{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_lastScale = 1.0;
}
CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = self.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
_lastScale = [(UIPinchGestureRecognizer*)sender scale];
;
}
</code></pre>
<p>非常感谢!!</p>
<p>我在谷歌上搜索了很多,发现了这个:</p>
<pre><code>self.layer.borderWidth = 2.0f / scaleFactor;
</code></pre>
<p>可悲的是,它对我不起作用……这很有意义,但不起作用。</p>
<p>我还阅读了有关在背面添加其他 View 并使前 View 在位置上有偏移的解决方案,以便显示后 View 并且看起来像边框。这不是一个选项,因为我需要我的图像以透明方式查看。 </p>
<p>我想重新创建 Aviary 在他们的应用程序中所做的事情。您可以放大和缩小“贴纸”,并且边框始终保持相同大小。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我在 Swift 4.2 中也试图以这种方式实现这种效果。最终我无法使用 CGAffine Effects 成功地做到这一点。我最终不得不更新约束——因为我正在使用 anchor 。</p>
<p>这是我最终开始使用的代码。 <code>SelectedView</code> 是应该缩放的 View 。</p>
<p>请记住,这不会破坏其他 CGAffine 效果。我仍在使用它们进行轮换。</p>
<pre><code>@objc private func scaleSelectedView(_ sender: UIPinchGestureRecognizer) {
guard let selectedView = selectedView else {
return
}
var heightDifference: CGFloat?
var widthDifference: CGFloat?
// increase width and height according to scale
selectedView.constraints.forEach { constraint in
guard
let view = constraint.firstItem as? UIView,
view == selectedView
else {
return
}
switch constraint.firstAttribute {
case .width:
let previousWidth = constraint.constant
constraint.constant = constraint.constant * sender.scale
widthDifference = constraint.constant - previousWidth
case .height:
let previousHeight = constraint.constant
constraint.constant = constraint.constant * sender.scale
heightDifference = constraint.constant - previousHeight
default:
return
}
}
// adjust leading and top anchors to keep view centered
selectedView.superview?.constraints.forEach { constraint in
guard
let view = constraint.firstItem as? UIView,
view == selectedView
else {
return
}
switch constraint.firstAttribute {
case .leading:
guard let widthDifference = widthDifference else {
return
}
constraint.constant = constraint.constant - widthDifference / 2
case .top:
guard let heightDifference = heightDifference else {
return
}
constraint.constant = constraint.constant - heightDifference / 2
default:
return
}
}
// reset scale after applying in order to keep scaling linear rather than exponential
sender.scale = 1.0
}
</code></pre>
<p>注意:宽度和高度 anchor 位于 View 本身。 Top 和Leadinganchor 在superview 上——因此有两个<code>forEach</code>block 。</p></p>
<p style="font-size: 20px;">关于ios - 将 CGAffineTransform Scale 应用于 UIView 使图层边框也变大,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24682706/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24682706/
</a>
</p>
页:
[1]