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

     

  • 相关阅读:
    Diocp截图
    [DIOCP视频]-DIOCPFileServer视频
    DIOCP 运作核心探密
    DIOCP-DIOCPv5的处理能力
    【被C折腾系列】用C调DIOCP编码客户端通信
    DIOCP-V5发布
    MyBean通用报表插件介绍
    Amazon
    输出所有大小写字符的组合
    删除最少字符生成Palindrome
  • 原文地址:https://www.cnblogs.com/CafeWing/p/3092546.html
Copyright © 2011-2022 走看看