Giter Site home page Giter Site logo

aliengalaxy / react-native-orientation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from c19354837/react-native-orientation

0.0 2.0 0.0 184 KB

Listen to device orientation changes in react-native and set preferred orientation on screen to screen basis.

Home Page: https://www.npmjs.com/package/react-native-orientation

License: ISC License

Java 43.58% Objective-C 38.92% JavaScript 13.02% Ruby 4.49%

react-native-orientation's Introduction

React Native Orientation

npm version

Listen to device orientation changes in React Native applications and set preferred orientation on a per screen basis. Works on both Android and iOS.

NOTE - PLEASE READ

The current version of this package on NPM's registry is not updated. In order to use this package with RN apps on versions 0.40+ you must point your package to the master branch of this repository or install it like so:

npm install --save react-native-orientation@git+https://github.com/yamill/react-native-orientation.git

Installing

npm install --save react-native-orientation@git+https://github.com/yamill/react-native-orientation.git

Linking Native Dependencies

Automatic Linking

react-native link react-native-orientation

Manual Linking

iOS

  1. Add node_modules/react-native-orientation/iOS/RCTOrientation.xcodeproj to your xcode project, usually under the Libraries group
  2. Add libRCTOrientation.a (from Products under RCTOrientation.xcodeproj) to build target's Linked Frameworks and Libraries list

Android

  1. In android/setting.gradle

    ...
    include ':react-native-orientation', ':app'
    project(':react-native-orientation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation/android')
    
  2. In android/app/build.gradle

    ...
    dependencies {
        ...
        compile project(':react-native-orientation')
    }
    
  3. Register module in MainApplication.java

    import com.github.yamill.orientation.OrientationPackage;  // <--- import
    
    public class MainApplication extends Application implements ReactApplication {
      ......
    
      @Override
      protected List<ReactPackage> getPackages() {
          return Arrays.<ReactPackage>asList(
              new MainReactPackage(),
              new OrientationPackage()      <------- Add this
          );
      }
    
      ......
    
    }

Configuration

iOS

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

#import "Orientation.h" // <--- import

@implementation AppDelegate

  // ...

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

@end

Android

Implement onConfigurationChanged method in MainActivity.java

    import android.content.Intent; // <--- import
    import android.content.res.Configuration; // <--- import

    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

To use the react-native-orientation package in your codebase, you should use the Orientation module:

import Orientation from 'react-native-orientation';
export default class AppScreen extends Component {
  // ...

  componentWillMount() {
    // The getOrientation method is async. It happens sometimes that
    // you need the orientation at the moment the JS runtime starts running on device.
    // `getInitialOrientation` returns directly because its a constant set at the
    // beginning of the JS runtime.

    const initial = Orientation.getInitialOrientation();
    if (initial === 'PORTRAIT') {
      // do something
    } else {
      // do something else
    }
  },

  componentDidMount() {
    // this locks the view to Portrait Mode
    Orientation.lockToPortrait();

    // this locks the view to Landscape Mode
    // Orientation.lockToLandscape();

    // this unlocks any previous locks to all Orientations
    // Orientation.unlockAllOrientations();

    Orientation.addOrientationListener(this._orientationDidChange);
  },

  _orientationDidChange = (orientation) => {
    if (orientation === 'LANDSCAPE') {
      // do something with landscape layout
    } else {
      // do something with portrait layout
    }
  },

  componentWillUnmount() {
    Orientation.getOrientation((err, orientation) => {
      console.log(`Current Device Orientation: ${orientation}`);
    });


    // Remember to remove listener
    Orientation.removeOrientationListener(this._orientationDidChange);
  }

  render() {
    // ...

    return (
      // ...
    )
  }
}

Orientation Events

addOrientationListener((orientation) => {});

orientation will return one of the following values:

  • LANDSCAPE
  • PORTRAIT
  • PORTRAITUPSIDEDOWN
  • UNKNOWN
removeOrientationListener((orientation) => {});
addSpecificOrientationListener((specificOrientation) => {});

specificOrientation will return one of the following values:

  • LANDSCAPE-LEFT
  • LANDSCAPE-RIGHT
  • PORTRAIT
  • PORTRAITUPSIDEDOWN
  • UNKNOWN
removeSpecificOrientationListener((specificOrientation) => {});

API

  • lockToPortrait()
  • lockToLandscape()
  • lockToLandscapeLeft()
  • lockToLandscapeRight()
  • unlockAllOrientations()
  • getOrientation((err, orientation) => {})
  • getSpecificOrientation((err, specificOrientation) => {})

react-native-orientation's People

Contributors

yamill avatar sh3rawi avatar mcrumm avatar vitaminwater avatar pjcabrera avatar youennpennarun avatar watert avatar daleljefferson avatar skv-headless avatar stoneman1 avatar brianjd avatar c19354837 avatar albertwchang avatar asafbrieferydev avatar eduardinni avatar jaysoo avatar jastanton avatar ncnlinh avatar nklhtv avatar gitim avatar thoblr avatar tunezola avatar chetstone avatar

Watchers

James Cloos 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.