GithubHelp home page GithubHelp logo

paulxuca / react-native-orientation-locker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wonday/react-native-orientation-locker

1.0 2.0 0.0 86 KB

A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation.

License: MIT License

Java 48.53% Objective-C 37.79% JavaScript 11.27% Ruby 2.41%

react-native-orientation-locker's Introduction

react-native-orientation-locker

npm

A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation. (cross-platform support)

Feature

  • lock screen orientation to PORTRAIT|LANDSCAPE-LEFT|PORTRAIT-UPSIDEDOWN|LANDSCAPE-RIGHT.
  • listen on orientation changing of device
  • get the current orientation of device

ChangeLog

v1.1.6

  1. catch unknown device orientation value
  2. when calling unlockAllOrientations(), forcibly unlock whether lock or not

v1.1.5

  1. add Orientation.isLocked() and Orientation.removeAllListeners()

v1.1.4

  1. Fix typescript declarations

v1.1.3

  1. add addLockListener/removeLockListener
  2. improve android orientation changed event sending condition

v1.1.2

  1. improve android orientation changed event timing

v1.1.1

  1. fix show "supported event type for deviceOrientationDidChange..." error in debug
  2. fix getAutoRotateState() code error

v1.1.0 BREAK CHANGE

  1. split addOrientationListener(function(orientation, deviceOrientation)) to addOrientationListener(function(orientation)) and addDeviceOrientationListener(function(deviceOrientation))
  2. make sure when lockToXXX and unlockAllOrientations resend UI orientation event
  3. remove setTimout from orientation listener
  4. add getAutoRotateState() for android
  5. add TypeScript define file

v1.0.22

  1. add getAutoRotateState() (android only)

v1.0.21

  1. add getDeviceOrientation()
  2. orientationDidChange return DeviceOrientation

[more]

Notice

RN 0.58 + Android target SDK 27 maybe cause Issue: java.lang.IllegalStateException: Only fullscreen activities can request orientation problem, see [#55] for a solution.

Installation

Using npm

npm install react-native-orientation-locker --save
react-native link react-native-orientation-locker

Using CocoaPods (iOS Only)

pod 'react-native-orientation-locker', :path => '../node_modules/react-native-orientation-locker/react-native-orientation-locker.podspec'

Consult the React Native documentation on how to install React Native using CocoaPods.

Configuration

iOS

Add the following to your project's AppDelegate.m:

+#import "Orientation.h"

@implementation AppDelegate

// ...

+- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
+  return [Orientation getOrientation];
+}

@end

Android

Add following to android/app/src/main/AndroidManifest.xml

      <activity
        ....
+       android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">

          ....

      </activity>

Implement onConfigurationChanged method (in MainActivity.java)

// ...

+import android.content.Intent;
+import android.content.res.Configuration;

public class MainActivity extends ReactActivity {

+   @Override
+   public void onConfigurationChanged(Configuration newConfig) {
+       super.onConfigurationChanged(newConfig);
+       Intent intent = new Intent("onConfigurationChanged");
+       intent.putExtra("newConfig", newConfig);
+       this.sendBroadcast(intent);
+   }

    // ......
}

Usage

Whenever you want to use it within React Native code now you can: import Orientation from 'react-native-orientation-locker';

import Orientation from 'react-native-orientation-locker';


  _onOrientationDidChange = (orientation) => {
    if (orientation == 'LANDSCAPE-LEFT') {
      //do something with landscape left layout
    } else {
      //do something with portrait layout
    }
  };

  componentWillMount() {
    //The getOrientation method is async. It happens sometimes that
    //you need the orientation at the moment the js starts running on device.
    //getInitialOrientation returns directly because its a constant set at the
    //beginning of the js code.
    var initial = Orientation.getInitialOrientation();
    if (initial === 'PORTRAIT') {
      //do stuff
    } else {
      //do other stuff
    }
  },

  componentDidMount() {

    Orientation.getAutoRotateState((rotationLock) => this.setState({rotationLock}));
    //this allows to check if the system autolock is enabled or not.

    Orientation.lockToPortrait(); //this will lock the view to Portrait
    //Orientation.lockToLandscapeLeft(); //this will lock the view to Landscape
    //Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations

    //get current UI orientation
    /*
    Orientation.getOrientation((orientation)=> {
      console.log("Current UI Orientation: ", orientation);
    });

    //get current device orientation
    Orientation.getDeviceOrientation((deviceOrientation)=> {
      console.log("Current Device Orientation: ", deviceOrientation);
    });
    */

    Orientation.addOrientationListener(this._onOrientationDidChange);
  },

  componentWillUnmount: function() {
    Orientation.removeOrientationListener(this._onOrientationDidChange);
  }

Events

  • addOrientationListener(function(orientation))

When UI orientation changed, callback function will be called. But if lockToXXX is called , callback function will be not called untill unlockAllOrientations. It can return either PORTRAIT LANDSCAPE-LEFT LANDSCAPE-RIGHT PORTRAIT-UPSIDEDOWN UNKNOWN When lockToXXX/unlockAllOrientations, it will force resend UI orientation changed event.

  • removeOrientationListener(function(orientation))

  • addDeviceOrientationListener(function(deviceOrientation))

When device orientation changed, callback function will be called. When lockToXXX is called, callback function also can be called. It can return either PORTRAIT LANDSCAPE-LEFT LANDSCAPE-RIGHT PORTRAIT-UPSIDEDOWN UNKNOWN

  • removeDeviceOrientationListener(function(deviceOrientation))

  • addLockListener(function(orientation))

When call lockToXXX/unlockAllOrientations, callback function will be called. It can return either PORTRAIT LANDSCAPE-LEFT LANDSCAPE-RIGHT UNKNOWN UNKNOWN means not be locked.

  • removeLockListener(function(orientation))

  • removeAllListeners()

Functions

  • lockToPortrait()
  • lockToLandscape()
  • lockToLandscapeLeft() this will lock to camera left home button right
  • lockToLandscapeRight() this will lock to camera right home button left
  • unlockAllOrientations()
  • getOrientation(function(orientation))
  • getDeviceOrientation(function(deviceOrientation))
  • getAutoRotateState(function(state)) (android only)
  • isLocked() (lock status by this library)

orientation can return one of:

  • PORTRAIT
  • LANDSCAPE-LEFT camera left home button right
  • LANDSCAPE-RIGHT camera right home button left
  • PORTRAIT-UPSIDEDOWN
  • UNKNOWN

Notice: PORTRAIT-UPSIDEDOWN not support at iOS now

react-native-orientation-locker's People

Contributors

fiznool avatar gp3gp3gp3 avatar gyfelton avatar hufkens avatar jsamr avatar michelegoh avatar mtshrmn avatar paulxuca avatar wonday avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.