SoFunction
Updated on 2025-04-12

iOS implements WeChat shake function

1. Description

You need to make an interface, imitate WeChat and shake it to obtain the interface for check-in function.
First, clarify the following points:

1. Vibration is required.
2. Need sound. (Prepare the mp3 sound effect)

2. Post the code here directly

/ Created by Shi Xiongwei on 16/7/29.
// Copyright © 2016 Shi Xiongwei. All rights reserved.//

#import ""
#import <AVFoundation/>
#import <AudioToolbox/>
#import <CoreAudio/>
@interface SignBoardViewController ()
{
}
@property (nonatomic,strong) AVAudioPlayer * audioPlayer;

@end

@implementation SignBoardViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  //Set navigation  [self makeNav];
}

#pragma mark Custom Nav- (void)makeNav
{
  [ setTitle:@"Daily check-in"];//Rewrite title  //Modify the navigation button and modify the response method   = CGRectMake(0, 0, 13, 20);
  [ setBackgroundImage:[UIImage imageNamed:@"NavBack"] forState:UIControlStateNormal];
   = 0;
   = NO;
   = [UIColor clearColor].CGColor;

  //Add click method  [ addTarget:self action:@selector(navLeftClick) forControlEvents:UIControlEventTouchUpInside];

  //Hide the button on the right  = YES;
}

#pragma mark nav left navigation button method rewrite, return button- (void)navLeftClick
{
  [self dismissViewControllerAnimated:YES completion:^{
    nil;
  }];
}

#pragma mark -
#pragma mark Click- (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event

{
  NSLog(@"Click, touch method, etc.");
  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

}

#pragma mark -
#pragma mark Shake start- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event

{

  NSLog(@"begin motion");

}

#pragma mark -
#pragma mark Shake end- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event
{
  NSLog(@"end motion");
  if (motion ==UIEventSubtypeMotionShake )
  {
    //Play sound effects    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);
    //Set vibration    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  }

}

#pragma mark -
#pragma mark shake Cancel- (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.
}
*/

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.