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

     

  • 相关阅读:
    canvas实现涂鸦板
    走进javascript——被忽视的DOM方法和属性
    走进javascript——DOM事件
    HTML5本地储存sessionStorage的销毁数据问题
    给微软的依赖注入框架写一些扩展方法
    一个支持 CodeFirst/DbFirst/ModelFirst 的数据库小工具
    用 C# 写一个 Redis 数据同步小工具
    SqlServer 利用游标批量更新数据
    WeihanLi.Npoi 根据模板导出Excel
    angular 构建可以动态挂载的配置服务
  • 原文地址:https://www.cnblogs.com/CafeWing/p/3092546.html
Copyright © 2011-2022 走看看