介绍
本篇文章给大家分享的是有关怎么在iOS中实现微信摇一摇功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
<强>一、描述强>
需要做一个界面,仿照微信摇一摇,获取接口进行签到功能。
首先明确以下几点:
1,需要震动。
2,需要声音。(准备好mp3音效)
<强>二,直接贴代码强>
/, Created by 石雄伟,提醒16/7/29。//,Copyright 及副本;,2016年,石雄伟只All rights 保留。//# import “SignBoardViewController.h" # import & lt; AVFoundation/AVFoundation.h> # import & lt; AudioToolbox/AudioToolbox.h> # import & lt; CoreAudio/CoreAudioTypes.h> @interface SignBoardViewController () { } @property (原子、强),AVAudioPlayer *, audioPlayer; @end @implementation SignBoardViewController 安康;(空白)viewDidLoad { (才能super viewDidLoad);//才能,Do any additional setup after loading 从而视图。//才能设置导航 (才能self makeNav); } # pragma mark 定制导航 安康;makeNav (void) { (才能self.navigationItem setTitle: @"每日签到“);//改写标题//修才能改导航按钮,并且修改响应方法 时间=self.leftButton.frame 才能;CGRectMake(0, 0, 13日,20); (才能self.leftButton setBackgroundImage: [UIImage imageNamed: @" NavBack"], forState: UIControlStateNormal); 时间=self.leftButton.layer.cornerRadius 才能;0; self.leftButton.layer.masksToBounds 才能,=,没有; 时间=self.leftButton.layer.borderColor 才能;[UIColor clearColor] .CGColor;//才能添加点击方法 (才能self.leftButton addTarget: self 行动:@ selector (navLeftClick), forControlEvents: UIControlEventTouchUpInside);//隐才能藏,右边按钮 self.rightButton.hidden才能=,是的, } # pragma mark  nav左边导航按钮方法重写,返回按钮 安康;navLeftClick (void) { (才能self dismissViewControllerAnimated: YES 完成:^ { ,,,零; ,,}]; } # pragma mark - # pragma mark 点击 安康;(空白)touchesBegan:(nonnull NSSet< UITouch *祝辞,*)touches withEvent: (nullable UIEvent *)事件 { NSLog才能(@"点击,触摸方法等“); AudioServicesPlaySystemSound才能(kSystemSoundID_Vibrate); } # pragma mark - # pragma mark 摇动开始 安康;(空白)motionBegan:(UIEventSubtype) motion withEvent: (nullable UIEvent *)事件 { NSLog才能(@" begin motion"); } # pragma mark - # pragma mark 摇动结束 安康;(空白)motionEnded:(UIEventSubtype) motion withEvent: (nullable UIEvent *)事件 { NSLog才能(@"最终获得;motion"); if 才能;(motion ==UIEventSubtypeMotionShake ) {才能 ,,,//播放音效 ,,,SystemSoundID , soundID;,//shake_sound_male.mp3 ,,,NSString * path =, [[NSBundle mainBundle ], pathForResource: @" shake_sound_male",减低:@" mp3"]; ,,,AudioServicesCreateSystemSoundID ((__bridge CFURLRef) (NSURL fileURLWithPath:路径),,,soundID); ,,,AudioServicesPlaySystemSound (soundID); ,,,//设置震动 ,,,AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); ,,} } # pragma mark - # pragma mark 摇动取消 安康;(空白)motionCancelled:(UIEventSubtype) motion withEvent: (UIEvent *)事件 { } 安康;(空白)didReceiveMemoryWarning { (才能super didReceiveMemoryWarning);//才能,Dispose of any resources that 还要be 重新创建。 }/* # pragma mark 安康;导航//,拷贝a storyboard-based 应用程序,,你will often want 用do a little preparation before 导航 安康;(空白)prepareForSegue:(UIStoryboardSegue *) segue 发送方:(id) sender {//才能,Get 从而new view controller using [segue destinationViewController]。//才能,Pass 从而selected object 用,new view 控制器。 } */怎么在iOS中实现微信摇一摇功能