• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 CGAffineTransform Scale 应用于 UIView 使图层边框也变大

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:46:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap