【问题标题】:Scaling a UIView from a point other than the center, looking for alternatives to anchorPoint从中心以外的点缩放 UIView,寻找 anchorPoint 的替代品
【发布时间】:2011-07-16 04:42:44
【问题描述】:

我一直在使用 CGAffineTransformMakeScale 来缩放 UIView。这将通过它的默认锚点进行缩放,即 (.5, .5) - 中心。我想缩放它,使其向左延伸,就好像它向左缩放一样。我可以通过将anchorPoint 设置为正确的中心来​​实现这一点。但是,这会导致设置框架等问题。有没有更好的方法来应用我想要的比例变换。我在想我可以使用自定义变换矩阵来做到这一点,但我不擅长矩阵数学。

【问题讨论】:

    标签: ios matrix transform calayer scale


    【解决方案1】:

    先运行这段代码,你就会有更好的理解。这里 lblLabel 是我的标签,你可以做相应的改变。还要检查 CGPoint(x: 10, y: 98) 和 CGPoint(x: 10, y: 108) 作为 par > 您的要求。 这将从标签的左上角开始动画。根据您的要求更改锚点和位置。



    self.lblLabel.layer.anchorPoint = CGPoint(x: 0.0,y :0.0)
            self.lblLabel.layer.position = CGPoint(x: 10, y: 98)
            lblLabel.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
    
            UIView.animate(withDuration: 1.0) {
                 self.lblLabel.layer.position = CGPoint(x: 10, y: 108)
                self.lblLabel.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
            }
    

    【讨论】:

      【解决方案2】:

      使用anchorPoint,绕过框架怪异,只需像this answer一样存储并重新加载它。

      【讨论】:

        猜你喜欢
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-21
        相关资源
        最近更新 更多