iOS实现微信摇一摇功能

 更新时间:2018年06月25日 14:59:37   作者:狮兄  
这篇文章主要为大家详细介绍了iOS实现微信摇一摇功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、描述

需要做一个界面,仿照微信摇一摇,获取接口进行签到功能。
首先明确以下几点:

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.
}
*/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 图文讲解如何解决App的iOS 7顶部状态栏适配问题

    图文讲解如何解决App的iOS 7顶部状态栏适配问题

    至今很多iOS App还要照顾到iOS 7进行最低的版本适配,而iOS7开始status bar的实际屏幕占用情况与iOS 6等不尽相同,这里我们就来以图文讲解如何解决App的iOS 7顶部状态栏适配问题
    2016-06-06
  • ios下OC与JS交互之WKWebView

    ios下OC与JS交互之WKWebView

    本篇文章主要介绍了ios下OC与JS交互之WKWebView,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • IOS笔记061之二维码的生成和扫描

    IOS笔记061之二维码的生成和扫描

    随着移动设备的普及为二维码提供了一个很好应用平台,无论是在商城购物还是美食城都离不开二维码,本篇文章就给大家介绍IOS笔记061之二维码的生成和扫描,感兴趣的朋友可以过来一起学习啦,本文内容讲的很详细
    2015-08-08
  • iOS状态栏的显示与隐藏的示例代码

    iOS状态栏的显示与隐藏的示例代码

    这篇文章主要介绍了iOS状态栏的显示与隐藏的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 运用iOS教你轻松制作音乐播放器

    运用iOS教你轻松制作音乐播放器

    这篇文章主要教大家如何运用iOS轻松制作音乐播放器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • iOS UIAlertController中UITextField添加晃动效果与边框颜色详解

    iOS UIAlertController中UITextField添加晃动效果与边框颜色详解

    这篇文章主要给大家介绍了关于iOS UIAlertController中UITextField添加晃动效果与边框颜色的相关资料,实现后的效果非常适合在开发中使用,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起看看吧。
    2017-10-10
  • iOS键盘自适应弹出效果

    iOS键盘自适应弹出效果

    这篇文章主要为大家详细介绍了iOS键盘自适应弹出效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • 将多个字符串高亮显示之TTTAttributedLabel

    将多个字符串高亮显示之TTTAttributedLabel

    本文介绍了将多个字符串高亮显示之TTTAttributedLabel。在此需要对每个字符串进行匹配,可以研究下kmp和bm算法,在这里应用了oc自带的NSRegularExpression 来进行正则表达式匹配,算是比较简单的方法,需要的朋友可以参考下
    2015-07-07
  • React Native学习教程之Modal控件自定义弹出View详解

    React Native学习教程之Modal控件自定义弹出View详解

    这篇文章主要给大家介绍了关于React Native学习教程之Modal控件自定义弹出View的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用React Native具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • IOS开发之JSON转PLIST实例详解

    IOS开发之JSON转PLIST实例详解

    这篇文章主要介绍了IOS开发之JSON转PLIST实例详解的相关资料,需要的朋友可以参考下
    2016-11-11

最新评论