ynizhenie
@ynizhenie

Как сделать Screen Orientation?

Добрый день как сделать проверку ориентации экрана?

Использую:
import { ScreenOrientation } from 'expo';

То есть когда аппарат делает поворот срабатывает ScreenOrientation.
  • Вопрос задан
  • 613 просмотров
Решения вопроса 1
ynizhenie
@ynizhenie Автор вопроса
Решение:

import { Component } from 'react';
import PropTypes from 'prop-types';
import Expo from 'expo';
import { expo } from '../app.json';

const ORIENTATION_DEFAULT = Expo.ScreenOrientation.Orientation.ALL;
const ORIENTATIONS = {
  'portrait': Expo.ScreenOrientation.Orientation.PORTRAIT,
  'landscape': Expo.ScreenOrientation.Orientation.LANDSCAPE,
  'default': ORIENTATION_DEFAULT,
};

export default class ScreenOrientation extends Component {
  allow(orientation) {
    const key = orientation === 'default' ? expo.orientation : orientation;
    Expo.ScreenOrientation.allow(ORIENTATIONS[key] || ORIENTATION_DEFAULT);
  }

  componentDidMount() {
    this.allow(this.props.allow);
  }

  componentWillUnmount() {
    this.allow('default');
  }

  render() {
    return this.props.children;
  }
}

ScreenOrientation.propTypes = {
  children: PropTypes.node,
  allow: PropTypes.oneOf(Object.keys(ORIENTATIONS)),
};
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы