iOS仿微信摇一摇功能

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

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

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

相关文章

  • iOS实现轮盘动态效果

    iOS实现轮盘动态效果

    这篇文章主要为大家详细介绍了iOS实现轮盘动态效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • iOS图片模糊效果的实现方法

    iOS图片模糊效果的实现方法

    这篇文章主要为大家详细介绍了iOS图片模糊效果的三种实现方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • 解决iOS下无法触发focus事件的问题

    解决iOS下无法触发focus事件的问题

    今天小编就为大家分享一篇解决iOS下无法触发focus事件的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • IOS登录页面动画、转场动画开发详解

    IOS登录页面动画、转场动画开发详解

    本篇文章通过详细的步骤给大家详细讲述了IOS登录页面动画、转场动画开发的详细教程,有兴趣的朋友参考学习下。
    2018-01-01
  • IOS 改变键盘颜色代码

    IOS 改变键盘颜色代码

    这篇文章主要介绍了IOS 改变键盘颜色代码,十分的简单实用,有需要的小伙伴可以参考下。
    2015-05-05
  • iOS集成微信支付开发

    iOS集成微信支付开发

    这篇文章主要为大家详细介绍了iOS集成微信支付开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • iOS开发之运动事件和远程控制

    iOS开发之运动事件和远程控制

    在iOS中事件分为三类:触摸事件:通过触摸、手势进行触发(例如手指点击、缩放),运动事件:通过加速器进行触发(例如手机晃动),远程控制事件:通过其他远程设备触发(例如耳机控制按钮)今天我们来详细探讨下运动事件和远程控制
    2016-04-04
  • iOS  隐藏导航条和状态栏实现方法

    iOS 隐藏导航条和状态栏实现方法

    这篇文章主要介绍了 iOS隐藏导航条和状态栏实现方法的相关资料,有时候根据需求开发APP 需要隐藏导航栏和状态栏,这里提供了实现方法需要的朋友可以参考下
    2016-11-11
  • iOS中UIScrollView嵌套UITableView的实践教程

    iOS中UIScrollView嵌套UITableView的实践教程

    在UIScrollView嵌套UITableView的问题相信大家都遇到过,小编最近在工作中就遇到了这个问题,所以这篇文章主要介绍了iOS中UIScrollView嵌套UITableView的相关资料,文中介绍的方法是通过自己的实践所得来的,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • 解决SDK注入权限验证安卓正常,IOS出现config fail的方法

    解决SDK注入权限验证安卓正常,IOS出现config fail的方法

    这篇文章主要介绍了解决SDK注入权限验证安卓正常,IOS出现config fail的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06

最新评论