Forum Archive

UI and Device Rotation Monitoring

cook

I think someone had figured out a way to monitor for device orientation change and subsequently redraw a presented ui.View. I can't find that in the forums..

I did find a way to do that with objc, but after starting venturing into objc land for a moment...I got lost.

Here's what I pulled from stack exchange... Sounds like the right thing to do.

// Listen for device orientation changes:

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

// When notified, get the device orientation from UIDevice:

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation)
    {
        // etc... 
    }
}
  1. I'm wondering how I can make this with objc_util
  2. How is this even implemented in a script? It seems like it should go inside a view class... But I have no idea.
brumm

I'm not sure if you're interested in a none objc way...

#overwrite these methods in a ui.View
def layout(self):
        if self.width > self.height:
            self.scr_orientation = 'landscape'
        else:
            self.scr_orientation = 'portrait'

def draw(self):
    #display your landscape or portrait content

cook

@brumm I am interested in any way that makes it possible.

Correct me if I'm wrong... Isn't your example just going to work when the view is initialized and presented at first?

I'm interested in after the view is presented, when I rotate the device the view is redrawn.

For example if I have a view set with a width of the screen and a label with label.x = view.width/3 ...
If I rotate the device, the screen width is now different but the original view.width remains the same.
I want to change the view.width on rotation and redraw the view so that everything in the view stays in the right place.

I think that this was discussed in the forums before... Can't find it. But yeah, any approach is fine! I just searched for this and found it on stack exchange, that's all.

Thanks for your response!

JonB

My RootView had a method for detecting rotation, using a hidden WebView. for responding to ui changes, you would use the layout method,mwhich gets called when your view is resized (due to rotation in a fullscreen or panel situation)

UINotifications are also a possibility. Here is a very simple UINotification listener which I used for capturing all notifications, ti explore what notifications are posted by the custom omz objects.
here.
This gives the basic idea -- though you would want to pass a specific notification name string as the first argument to
addObserverForName_object_queue_usingBlock_
and probably would also pass your ui objects _objc_ptr as the second, so that the system does the filtering for you. If you are listening to a specific ui.View, you might be able to better handle the cleanup (my weakref attempts did not work)

JonB

@cook For simple layouts, you would use the flex attribute. For just scaling to size, this can work well, though may require many layers of Views as containers for fine control over how things flex.
If you need to change which components actually appear,mthe Custom View layout method is what you want.

brumm
# coding: utf-8
import ui

class myView(ui.View):
    def __init__(self):
        self.scr_orientation = None
        self.present('full_screen')

    def draw(self):
        print 'orientation = ' + self.scr_orientation

    def layout(self):
        if self.width > self.height:
            self.scr_orientation = 'landscape'
        else:
            self.scr_orientation = 'portrait'

myView()
#orientation = landscape
#or
#orientation = portrait
#there's no orientation = None
#and everytime I tilt my device it changes...
cook

@JonB @brumm thanks for writing. Sorry for my late reply.

Yes @JonB I think it would work with these things, but it's tough. Hence my confusion!!

@brumm I'm sorry I didn't take the time before to really try what you suggested. It looks good. I'm going to add that into/modify what I have going and see what I can get.

Really appreciate the help guys!