ios 实现摇一摇并震动

来源:互联网 发布:java 服务端开发 编辑:程序博客网 时间:2024/06/06 14:15


iphone 手机实现摇一摇并震动,代码网上都有


使用时需要注意:


1、添加AudioToolbox.framework

2、导入头文件

#import <AudioToolbox/AudioServices.h>


代码如下:



- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];    [self becomeFirstResponder];   }


- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{        //检测到摇动    NSLog(@"开始摇动了");    }- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{    //摇动结束    if (event.subtype == UIEventSubtypeMotionShake) {        //something happens        [self test];            }else{        NSLog(@"好怪异啊");    }    }- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{    //摇动取消    NSLog(@"取消摇动了");}- (BOOL)canBecomeFirstResponder {        return YES;    }#import <AudioToolbox/AudioServices.h>-(void)test{        NSLog(@"完成摇动后处理事件");    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);    }


0 0
原创粉丝点击