传感器--------屏幕方向检测

本文介绍了一个iOS应用如何通过监听屏幕方向变化的通知来检测设备的方向,并提供了详细的代码示例。通过switch case语句实现了对不同方向的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先添加通知, 记得在控制器销毁时移除通知就可以了,  不同方向需要实现的代码直接下载switch  case  语句中就可以了

//
//  ViewController.m
//  屏幕方向检测
//
//  Created by Wangjunling on 16/4/6.
//  Copyright © 2016年 Wangjunling. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加屏幕方向改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationDidChange {
    switch ([UIDevice currentDevice].orientation) {
        case 0:
            NSLog(@"UIDeviceOrientationUnknown");//默认
            break;
        case 1:
            NSLog(@"UIDeviceOrientationPortrait");//垂直向上
            break;
        case 2:
            NSLog(@"UIDeviceOrientationPortraitUpsideDown");//垂直向下
            break;
        case 3:
            NSLog(@"UIDeviceOrientationLandscapeLeft");//垂直向左
            break;
        case 4:
            NSLog(@"UIDeviceOrientationLandscapeRight");//垂直向右
            break;
        case 5:
            NSLog(@"UIDeviceOrientationFaceUp");//平放屏幕向上
            break;
        case 6:
            NSLog(@"UIDeviceOrientationFaceDown");//平放屏幕朝下
            break;
        default:
            break;
    }
}
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值