Swift 中 iOS7 和 iOS8/iOS9 上的仿射变换缩放比例不同 [英] Scale affine transformation scaling differently on iOS7 and iOS8/iOS9 in Swift

查看:83
本文介绍了Swift 中 iOS7 和 iOS8/iOS9 上的仿射变换缩放比例不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Swift 中使用缩放仿射变换,并注意到 CGAffineTransformMakeScale 在所有 iOS 版本上的工作方式不同.为了演示差异,我创建了一个新的 Xcode 7 项目,在运行于 iOS7 设备、iOS8 模拟器和 iOS9 模拟器的 Xcode Storyboard 上设置了三个测试框.

I am using scale affine transformations in Swift and have noticed CGAffineTransformMakeScale does not work the same on all iOS versions. To demonstrate the differences, I have created a new Xcode 7 project, set up three test boxes on the Xcode Storyboard running on an iOS7 device, iOS8 simulator and iOS9 simulator.

框 A - 没有应用约束,位于故事板的顶部中央

框 B - 设置了高度和宽度以及中心水平和垂直中心对齐约束.

框 C - 设置了高度和宽度以及底部空间和中心水平对齐约束.

然后使用以下代码将框缩放到 0.5.

注意:粉色区域不是框或容器,而是用于在发生尺度仿射变换后突出蓝色框的位置.

结果:

iOS7 存在问题 - 虽然所有盒子的大小都减半,但 A 和 C 两个盒子不会保持居中.

iOS7 there are problems- while all boxes halve their size, two boxes, A and C, don’t remain centred in place.

iOS8/iOS9 按预期工作 - 无论是否应用约束,所有框都将其大小减半并保持居中.

iOS8/iOS9 works as expected- all boxes halve their size and remain centered in place whether or not constraints are applied.

问题:

导致此问题的原因是什么,如何最好地纠正和解决它,以便所有 iOS7/8/9 版本都可以相同地工作?

What is causing this problem and how can it be best corrected and solved so that all iOS7/8/9 versions work identically?

CGAffineTransformMakeScale 在 iOS7 上 - 无法按预期缩放 :-(

CGAffineTransformMakeScale on iOS7 - does not scale as expected :-(

CGAffineTransformMakeScale 在 iOS8/iOS9 上 - 按预期缩放 :-)

CGAffineTransformMakeScale on iOS8/iOS9 - scales as expected :-)

代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var ButtonA: UIButton!
    @IBOutlet weak var ButtonB: UIButton!
    @IBOutlet weak var ButtonC: UIButton!

    @IBAction func ButtonScale(sender: AnyObject) {
        self.ButtonA.transform = CGAffineTransformMakeScale(0.5, 0.5)
        self.ButtonB.transform = CGAffineTransformMakeScale(0.5, 0.5)
        self.ButtonC.transform = CGAffineTransformMakeScale(0.5, 0.5)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

推荐答案

这由 描述约束和变换——自动布局如何在 iOS 8 中悄悄变得对转换友好.

基本上,在 iOS7 及更早版本中,您不应对位于转换不同侧的值设置约束.

Basically, in iOS7 and older you should not set constraints on values that are on different sides of a transformation.

这篇关于Swift 中 iOS7 和 iOS8/iOS9 上的仿射变换缩放比例不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆