iOS实现微信摇一摇功能
程序员文章站
2023-12-17 10:32:40
一、描述
需要做一个界面,仿照微信摇一摇,获取接口进行签到功能。
首先明确以下几点:
1、需要震动。
2、需要声音。(准备好mp3音效)
二...
一、描述
需要做一个界面,仿照微信摇一摇,获取接口进行签到功能。
首先明确以下几点:
1、需要震动。
2、需要声音。(准备好mp3音效)
二、这边直接贴代码
/ created by 石雄伟 on 16/7/29. // copyright © 2016年 石雄伟. all rights reserved. // #import "signboardviewcontroller.h" #import <avfoundation/avfoundation.h> #import <audiotoolbox/audiotoolbox.h> #import <coreaudio/coreaudiotypes.h> @interface signboardviewcontroller () { } @property (nonatomic,strong) avaudioplayer * audioplayer; @end @implementation signboardviewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. //设置导航 [self makenav]; } #pragma mark 定制nav - (void)makenav { [self.navigationitem settitle:@"每日签到"];//改写title //修改导航按钮,并且修改响应方法 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 = no; self.leftbutton.layer.bordercolor = [uicolor clearcolor].cgcolor; //添加点击方法 [self.leftbutton addtarget:self action:@selector(navleftclick) forcontrolevents:uicontroleventtouchupinside]; //隐藏 右边按钮 self.rightbutton.hidden= yes; } #pragma mark nav左边导航按钮方法重写,返回按钮 - (void)navleftclick { [self dismissviewcontrolleranimated:yes completion:^{ nil; }]; } #pragma mark - #pragma mark 点击 - (void)touchesbegan:(nonnull nsset<uitouch *> *)touches withevent:(nullable uievent *)event { nslog(@"点击,触摸方法等"); audioservicesplaysystemsound(ksystemsoundid_vibrate); } #pragma mark - #pragma mark 摇动开始 - (void)motionbegan:(uieventsubtype)motion withevent:(nullable uievent *)event { nslog(@"begin motion"); } #pragma mark - #pragma mark 摇动结束 - (void)motionended:(uieventsubtype)motion withevent:(nullable uievent *)event { nslog(@"end motion"); if (motion ==uieventsubtypemotionshake ) { //播放音效 systemsoundid soundid; // shake_sound_male.mp3 nsstring *path = [[nsbundle mainbundle ] pathforresource:@"shake_sound_male" oftype:@"mp3"]; audioservicescreatesystemsoundid((__bridge cfurlref)[nsurl fileurlwithpath:path], &soundid); audioservicesplaysystemsound (soundid); //设置震动 audioservicesplaysystemsound(ksystemsoundid_vibrate); } } #pragma mark - #pragma mark 摇动取消 - (void)motioncancelled:(uieventsubtype)motion withevent:(uievent *)event { } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } /* #pragma mark - navigation // in a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // get the new view controller using [segue destinationviewcontroller]. // pass the selected object to the new view controller. } */
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。