zoukankan      html  css  js  c++  java
  • IOS 检测摇动

    有两种方式检测摇动:

    1. 继承UIWindow

    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
    }
    
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (motion == UIEventSubtypeMotionShake )
        {
            // User was shaking the device. Post a notification named "shake".
            [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];//消息注册
        }
    }
    
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {    
    }

    2. 在AppDelegate.m中

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        ...
        application.applicationSupportsShakeToEdit = YES;//添加此处
        ...
        return YES;
    }

    然后在viewcontroller中

    -(BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [self becomeFirstResponder];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [self resignFirstResponder];
        [super viewWillDisappear:animated];
    }
    
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (motion == UIEventSubtypeMotionShake)
        {
            NSLog(@"摇啊摇");
        }
    }

    原文:
    http://ilewen.com/questions/778/%E7%A8%8B%E5%BA%8F%E6%80%8E%E4%B9%88%E6%A3%80%E6%B5%8B%E7%94%A8%E6%88%B7%E5%9C%A8%E6%91%87%E5%8A%A8iphone%EF%BC%9F

  • 相关阅读:
    四种常用正则表达式
    解读tomcat的server.xml文件
    存储过程与函数的区别
    Servlet如何处理一个请求?
    Oracle归档模式相关
    常用几个Struts2标签
    Js中比较时间大小
    传值出现中文乱码问题
    JAVA常用转义符
    HDU 5967(LCT)
  • 原文地址:https://www.cnblogs.com/hubj/p/2569988.html
Copyright © 2011-2022 走看看