+
96
-

微信小程序或app面对面摇一摇如何实现匹配与邀请?

现在拼多多、美团、淘宝都搞现金活动,邀请用户一起面对面摇一摇就能分享红包,红包达到100就可以取现,请问微信小程序或app面对面摇一摇如何实现匹配与邀请?

网友回复

+
16
-

一、微信小程序或app如何检测摇一摇事件

首先我们来实现小程序或app如何实现摇一摇的检测,首先看微信小程序,微信小程序有个api重力感应接口,我们只要监听一下数据就可以检测用户是否摇一摇,代码如下:

Page({
  isShow: false,
  onShow: function () {
    var that = this;
    this.isShow = true;
    wx.onAccelerometerChange(function (e) {
      if(!that.isShow){
        return
      }
      console.log(e.x)
      console.log(e.y)
      console.log(e.z)
      if (e.x > 1 && e.y > 1) {
        wx.showToast({
          title: '摇一摇成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  onHide: function(){
    this.isShow = false;
  }
})

好了,那么andriod和ios如何实现呢

先看ios如何实现摇一摇检测, 新建一个CWindow类继承UIWindow,重写-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event方法。 .h文件代码:

#import <UIKit/UIKit.h>
 
@interface CWindow : UIWindow
 
@end

.m文件代码:

//
//  CWindow.m
//  Contact
//
 
#import "CWindow.h"
#import "CConstants.h"
 
@implementation CWindow
 
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
 
 
 
//检测摇动
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        CCLog(@"Motion shake.");
        
        [[NSNotificationCenter defaultCenter] postNotificationName:APP_SHAKE_NOTIFICATION object:nil];
        
    }
}
 
/*
// Only override dra...

点击查看剩余70%

我知道答案,我要回答