2016-06-23 10 views

Antwort

2

Basierend auf einer Logik gefunden in here der KIF testing framework, ich nehme an, es sollte möglich sein.

Der Objective-C-Code hinter dem Link sieht wie folgt aus:

- (void)simulateDeviceRotationToOrientation:(UIDeviceOrientation)orientation 
{ 
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:orientation] forKey:@"orientation"]; 
} 

Der Swift Äquivalent dieser Code würde wie folgt aussehen:

func simulateDeviceRotation(toOrientation orientation: UIDeviceOrientation) { 
    let orientationValue = NSNumber(integer: orientation.rawValue) 
    UIDevice.currentDevice().setValue(orientationValue, forKey: "orientation") 
} 

Und dann würden wir nennen es einfach etwas wie folgt:

simulateDeviceRotation(toOrientation: .LandscapeLeft) 

Oder möchten wir vielleicht eine Funktion, die Code für jede Ausrichtung ausführt?

func forEachOrientation(block:() -> Void) { 
    for orientation in [UIDeviceOrientation.Portrait, UIDeviceOrientation.LandscapeLeft, UIDeviceOrientation.PortraitUpsideDown, UIDeviceOrientation.LandscapeRight] { 
     simulateDeviceRotation(toOrientation: orientation) 
     block() 
    } 
} 

Und wir können es einfach so nennen:

forEachOrientation { 
    // do a thing 
} 

Aus meiner Erfahrung, dies scheint tatsächlich nicht auf iOS 9 Simulatoren zu arbeiten, aber es funktioniert auf iOS 8 Simulatoren. Ich weiß nicht, ob das auf echten Geräten funktionieren würde.