OGeek|极客世界-中国程序员成长平台

标题: ios - 将 CGAffineTransform Scale 应用于 UIView 使图层边框也变大 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:46
标题: ios - 将 CGAffineTransform Scale 应用于 UIView 使图层边框也变大

我有一个 UIView,里面有一个 UIImageView。我在 UIVIew 中添加了一个 UIPinchGestureRecognizer 来处理捏合和缩放,并使 UIView 与 UIImageView 一起增长。

我的 UIView 有一个边框。我以这种方式添加了边框:

self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 1.0f;
self.layer.cornerRadius = 8.0f;

而我遇到的问题是,我找不到在保持边框宽度相同的同时使 UIView 更大的方法。捏合和缩放时,边框会变粗。

这是我的 UIPinchGestureRecognizer 处理程序:

- (void)scaleUIPanGestureRecognizer *)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];

    [self setTransform:newTransform];
}

非常感谢!!

我在谷歌上搜索了很多,发现了这个:

self.layer.borderWidth = 2.0f / scaleFactor;

可悲的是,它对我不起作用……这很有意义,但不起作用。

我还阅读了有关在背面添加其他 View 并使前 View 在位置上有偏移的解决方案,以便显示后 View 并且看起来像边框。这不是一个选项,因为我需要我的图像以透明方式查看。

我想重新创建 Aviary 在他们的应用程序中所做的事情。您可以放大和缩小“贴纸”,并且边框始终保持相同大小。



Best Answer-推荐答案


我在 Swift 4.2 中也试图以这种方式实现这种效果。最终我无法使用 CGAffine Effects 成功地做到这一点。我最终不得不更新约束——因为我正在使用 anchor 。

这是我最终开始使用的代码。 SelectedView 是应该缩放的 View 。

请记住,这不会破坏其他 CGAffine 效果。我仍在使用它们进行轮换。

@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
}

注意:宽度和高度 anchor 位于 View 本身。 Top 和Leading anchor 在superview 上——因此有两个forEach block 。

关于ios - 将 CGAffineTransform Scale 应用于 UIView 使图层边框也变大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682706/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4