Exclude UIView from animation during autorotation

TL DR

Need to save autorotation, but exclude one UIView from autorotation when changing orientation, how?

Background story

I need to keep the UIView motionless during the animation followed by autorotation (which happens when the orientation changes). Just like the iOS camera app handles rotation (for example, controls rotate in place).

Things i tried

  • Returning false from shouldAutorotate() , subscribing to UIDeviceOrientationDidChangeNotification and attempting to manually handle the rotation event for each view separately.

    It works well if you do not need to change any places in your UIViews, otherwise the pain will figure out where it should end and how to find it

  • Placing a non-rotating UIWindow under the main UIWindow and setting the foreground color of the UIWindow interface.

    This works well if it is only one item, but I do not want to control the UIWindows bundle

  • Reverse rotation I. Rotation of the UIView in the opposite direction of rotation. Not reliable and looks strange, it also causes dizziness

  • Overriding animations in the viewWillTransitionToSize method. Failed

  • And a bunch of other things that will be difficult to list here, but they all failed .

Question

Can this be done? if so, how?

I support iOS8 +


Update . Here's what the views / oriented views should look like @Casey example :

enter image description here

+5
source share
3 answers

I ran into the same problem and found an example from Apple that helps prevent rotation of the UIView: https://developer.apple.com/library/ios/qa/qa1890/_index.html

However, if the UIView does not fit in the center of the screen, you must handle the new position manually.

+2
source

I think the reason is that it is so difficult to answer, because in practice it does not make sense.

let's say I'm pretending to use autostart to look like this in portrait and landscape orientations:

standard layout

if you want c to not spin, how do you ask what you expect from the final view? will it be one of these 3 options?

wonky layout

without the graphics of the portrait / landscape view you are trying to achieve, and the description of the animation you are hoping for, it will be very difficult to answer your question.

Do you NSLayoutConstraint , storyboard or frame math to layout your views? any code you can provide will be great too.

0
source

If you want to have the same effect as the camera app, use size classes (see here and here ).

If not, what's wrong with creating a UIWindow containing a view controller that doesn't rotate? The following code seems to work for me (where UILabel represents a view you don't want to rotate).

 class ViewController: UIViewController { var staticWindow: UIWindow! override func viewDidLoad() { super.viewDidLoad() showWindow() } func showWindow() { let frame = CGRect(x: 10, y: 10, width: 100, height: 100) let vc = MyViewController() let label = UILabel(frame: frame) label.text = "Hi there" vc.view.addSubview(label) staticWindow = UIWindow(frame: frame) staticWindow.rootViewController = MyViewController() staticWindow.windowLevel = UIWindowLevelAlert + 1; staticWindow.makeKeyAndVisible() staticWindow.rootViewController?.presentViewController(vc, animated: false, completion: nil) } } class MyViewController: UIViewController { override func shouldAutorotate() -> Bool { return false } override func shouldAutomaticallyForwardRotationMethods() -> Bool { return false } override func shouldAutomaticallyForwardAppearanceMethods() -> Bool { return false } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } } 
0
source

Source: https://habr.com/ru/post/1242262/


All Articles