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发出通知,在需要用到的地方来接受通知,实现我们需要的功能,这里就不在细讲了。

     

  • 相关阅读:
    【docker学习一】CentOS7.5+Docker安装及使用「安装、查看、pull、创建、进入镜像」
    excel for mac打开csv文件不分列
    【java自定义注解2】java自定义注解结合Spring AOP
    【java自定义注解1】java自定义注解-属性
    【jar包管理】Maven BOM
    【JAVA8】Set排序四种写法
    PAT甲题题解-1072. Gas Station (30)-dijkstra最短路
    PAT甲题题解-1073. Scientific Notation (20)-字符串处理
    PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
    PAT甲题题解-1075. PAT Judge (25)-排序
  • 原文地址:https://www.cnblogs.com/CafeWing/p/3092546.html
Copyright © 2011-2022 走看看