How to determine rotation for a software-generated UIView

I have UIViewone that is programmatically created:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];

        UIImageView* closeButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"modal_close.png"]];

        closeButton.frame = CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09, closeButton.frame.size.width, closeButton.frame.size.height);

        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09,20, 20)];

        [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];

        UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width*.1,self.frame.size.height*.1,self.frame.size.width*.8,self.frame.size.height*.8)];

        mainView.backgroundColor = [UIColor whiteColor];
        _displayMainView = mainView;        

        [self addSubview:_displayMainView];
        [self addSubview:closeButton];
        [self addSubview:button];

        mainView = nil;
        closeButton = nil;
    }
    return self;
}

How to determine the rotation? This should act as modal on top of another existing view. This is an iPad app, if that matters.

+5
source share
3 answers

I think you have two options:

  • You put the view in a custom view UIViewControllerand override the method shouldAutorotateToInterfaceOrientation:to return YES for all orientations that you want to support. The controller will automatically rotate and resize the content (your view). You just need to make sure that you have the correct autoresizeMask (or restrictions if you use autostart).
  • . , .

, , .

+5

:

  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

:

      [[NSNotificationCenter defaultCenter] addObserver: self selector:   @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];

- (void)deviceOrientationDidChange:(NSNotification *)notification {
     //Obtain current device orientation
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

      //Do my thing
 }

, dealloc UIView ( ) .

-(void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

?

+42

@clearwater82 Swift 3, - , :

init ( viewDidLoad):

// Handle rotation
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(
    self,
    selector: #selector(self.orientationChanged(notification:)),
    name: NSNotification.Name.UIDeviceOrientationDidChange,
    object: nil
)


orientationChanged . , , :

// Called when device orientation changes
@objc func orientationChanged(notification: Notification) {
    // handle rotation here
}


, deinit :

deinit {
    NotificationCenter.default.removeObserver(self)
    UIDevice.current.endGeneratingDeviceOrientationNotifications()
}

, , . , , .

, ,

+3

All Articles