Scale affine transformation scaling differently on iOS7 and iOS8/iOS9 in Swift

被刻印的时光 ゝ 提交于 2019-12-23 19:14:07

问题


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.

Box A - has no constraints applied and is positioned centre top on the Storyboard

Box B - has height and width set along with center horizontal and center vertical alignment constraints.

Box C - has height and width set along with bottom space and center horizontal alignment constraints.

Boxes are then scaled to 0.5 using the below code.

Note: the pink areas aren't boxes or containers, but are used to highlight the position of the blue boxes after a scale affine transformation has occurred.

Results:

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

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

Questions:

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


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

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


Code:

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()
    }

}

回答1:


This is described by Constraints & transformations - How Auto Layout quietly became transform-friendly in iOS 8.

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



来源:https://stackoverflow.com/questions/35207177/scale-affine-transformation-scaling-differently-on-ios7-and-ios8-ios9-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!