zoukankan      html  css  js  c++  java
  • 摇一摇功能的实现

    之前看到好多摇一摇的功能,后来发现在ios本身的UIResponder中提供了这一系列的方法,利用这些方法就能实现摇一摇的功能。

    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

    利用UIResponder中提供了这一系列的方法,有2个路径可以实现摇一摇的功能。

    第一种方法:

    - (void)viewDidLoad

    {

        [superview DidLoad];

        [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];//让viewcontroller支持摇动

    }


    - (void)viewDidAppear:(BOOL)animated {
        [self becomeFirstResponder];
    }

    - (BOOL)canBecomeFirstResponder {
        return YES;
    }

     

    然后去实现那几个方法就可以了

    - (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

    {

        //检测到摇动

    }

     

    - (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

    {

        //摇动取消

    }

     

    - (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

    {

        //摇动结束

        //可以执行系统震动操作-每个调用都会生成一个简短的1~2秒的震动。在不支持震动的平台上(ipod touch),该调用不执行任何操作,但也不会发生错误
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  

        if (event.subtype == UIEventSubtypeMotionShake) {

            //something happens

        }

    }

    注意:使用AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 需要包含AudioToolbox.framework包以及包含 #import <AudioToolbox/AudioToolbox.h>头文件

    另外还有一种方法

    另一种方法就需要我们来自定义UIWindow,在定义好的window的.m文件来实现摇一摇的上述方法。

    同时利用NSNotificationCenter发出通知,在需要用到的地方来接受通知,实现我们需要的功能,这里就不在细讲了。

     

  • 相关阅读:
    StrToInt && StrToHex && IntToString && 编辑框格式 .
    DAO简介(MFC) .
    RedrawWindow, UpdateWindow,InvalidateRect 用法
    在MFC中获取窗口\视图句柄 &获取当前活动的CView .
    DAO在MFC中的应用实例 .
    捕获异常 .
    ubuntu下chm和PDF阅读器 « 小居
    QQ登陆功能的实现2 rolends1986 博客园
    【原创】说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 随它去吧 博客园
    PDF Ubuntu中文
  • 原文地址:https://www.cnblogs.com/CafeWing/p/3092546.html
Copyright © 2011-2022 走看看