Adrian
Adrian

Reputation: 20068

How to unit test UIView's frame after applying CGAffineTransform(translationX)

I want to test the following method using Quick:

func initial(states: UIView...) {
    for view in states {
        view.transform = CGAffineTransform(translationX: 0, y: 20)
    }
}

This is the code I am using for testing (simplified version):

class ProtocolsSpec: QuickSpec {    
    override func spec() {
        var view1: UIView!
        var view2: UIView!

        describe("Animator") {
            beforeEach {
                view1 = UIView(frame: CGRect(x: 50, y: 50, width: 50, height: 50))
                view2 = UIView(frame: CGRect(x: 80, y: 80, width: 50, height: 50))
            }

            it("views origin should be (0, 20) at startup") {
                initial(states: view1, view2)

                expect(view1.frame.origin.x) == 0
                expect(view1.frame.origin.y) == 20
            }
        }
    }
}

The tests doesn't work, the x and y are not changed.
My question is how to get the correct (x,y) coordinates after applying the transformation ?

Upvotes: 0

Views: 338

Answers (1)

mAu
mAu

Reputation: 2018

The frame, center, and bounds properties will not change after applying transformations. The documentation of frame even states that:

If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

API Reference

In order to get the effective frame I'd suggest applying the views transform onto the views frame (using the applying(_:) method on CGRect. In your case that would look like:

let resultingFrame = view1.frame.applying(view1.transform)
expect(resultingFrame.origin.x) == 0
expect(resultingFrame.origin.y) == 20

Upvotes: 3

Related Questions