年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2124|回复: 0

iOS摇一摇的使用

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2015-4-21 12:13:01 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2015-4-21 12:14 编辑

    一、实现系统自带的motionBegan方法
    [Objective-C] 纯文本查看 复制代码
    /// 开始
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
    /// 结束
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
    /// 取消
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);


    假如程序不响应此方法,再实现以下方法:
    [Objective-C] 纯文本查看 复制代码
    - (BOOL)canBecomeFirstResponder
    {
        return YES;
    }


    二、motionBegan+通知的方法(建议)
    1.在Appdelegate里写motionBegan方法
    [Objective-C] 纯文本查看 复制代码
    -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SAMotionBegan" object:@(motion)];
    }


    2.在需要接收通知的页面添加通知
    [Objective-C] 纯文本查看 复制代码
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [SANotice addObserver:self selector:@selector(shake) name:@"SAMotionBegan" object:nil];
    }
    
    - (void)shake
    {
        SALog(@"摇一摇开始...");
    }
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-28 13:31 , Processed in 0.052926 second(s), 22 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表