美文网首页
微信摇一摇

微信摇一摇

作者: YYYLynn | 来源:发表于2016-03-01 14:17 被阅读119次

最近有做到摇一摇, 发现还是很简单的, 随手一记
主要实现的功能是: 开始要摇动的时候, 图片会向两边分开, 并且会有声音, 出现动画, 结束后, 图片合并, 动画消失,结束声音。
首先是系统框架: <AVFoundation/AVFoundation.h>, 用于声音的播放。

viewDidLoad

// 上半部分的图
imgUp = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 270 * KScreenWidth / 375)];
imgUp.image = [UIImage imageNamed:@"ShakeUp1"];
[self.view addSubview:imgUp];

// 下半部分的图
imgDown = [[UIImageView alloc]initWithFrame:CGRectMake(0, imgUp.y + imgUp.height , KScreenWidth, KScreenHeight - 64 - imgUp.y - imgUp.height)];
imgDown.image = [UIImage imageNamed:@"ShakeDown1"];
[self.view addSubview:imgDown];
// 中间的 GIF
imgGif = [[UIImageView alloc]initWithFrame:CGRectMake((KScreenWidth - 100 * KScreenWidth / 375) / 2, 210 * KScreenWidth / 375, 100 * KScreenWidth / 375, 100 * KScreenWidth / 375)];
imgGif.image = [UIImage imageNamed:@"1"];
[self.view addSubview:imgGif];

// 装载图片的数组
NSMutableArray *imageArray = [NSMutableArray array];
for (int i = 0; i < 6; i ++)
{
    NSString *imageString = [NSString stringWithFormat:@"%d", i + 1];
    UIImage *image = [UIImage imageNamed:imageString];
    [imageArray addObject:image];
}
imgGif.animationImages = imageArray;
imgGif.animationDuration = 0.2;
imgGif.animationRepeatCount = FLT_MAX;
imgGif.hidden = YES;

// 获得 self.view 的所有子视图
NSArray *arr=[self.view subviews];
// 将 imgGif 放到最下面
[self.view insertSubview:[arr lastObject] belowSubview:[arr firstObject]];


// 摇一摇
[[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];
[self becomeFirstResponder];

摇一摇还要实现三个方法:

// 检测到摇一摇
、、、
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"开始");
if (event.subtype == UIEventSubtypeMotionShake)
{
[UIView animateWithDuration:0.4 animations:^{
imgUp.image = [UIImage imageNamed:@"ShakeUp"];
imgDown.image = [UIImage imageNamed:@"ShakeDown"];
imgUp.y = -70 * KScreenWidth / 375;
imgDown.y = KScreenHeight - 64 - 263 * KScreenWidth / 375;
imgGif.hidden = NO;
[imgGif startAnimating];
}];
NSString *soundFielPath= [[NSBundle mainBundle]pathForResource:@"shake_sound_male" ofType:@"mp3"];
NSURL *fileURL=[[NSURL alloc]initFileURLWithPath:soundFielPath];
AVAudioPlayer *newPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
player=newPlayer;
[player prepareToPlay];
//开始播放
[player play];
}
}

// 摇一摇取消
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"取消");
[UIView animateWithDuration:0.4 animations:^{
imgUp.y = 0;
imgDown.y = 270 * KScreenWidth / 375;
imgUp.image = [UIImage imageNamed:@"ShakeUp1"];
imgDown.image = [UIImage imageNamed:@"ShakeDown1"];
} completion:^(BOOL finished) {
[imgGif stopAnimating];
imgGif.hidden = YES;
}];
}

// 摇一摇结束
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[UIView animateWithDuration:0.4 animations:^{
imgUp.y = 0;
imgDown.y = 270 * KScreenWidth / 375;
imgUp.image = [UIImage imageNamed:@"ShakeUp1"];
imgDown.image = [UIImage imageNamed:@"ShakeDown1"];
} completion:^(BOOL finished) {
[imgGif stopAnimating];
imgGif.hidden = YES;
num -= 1;
if (num <= 0)
{
num = 0;
}
label.text = [NSString stringWithFormat:@"摇一摇有惊喜, 今日剩余%ld次", (long)num];
}];
NSString *soundFielPath= [[NSBundle mainBundle]pathForResource:@"shake_match" ofType:@"mp3"];
NSURL *fileURL=[[NSURL alloc]initFileURLWithPath:soundFielPath];
AVAudioPlayer *newPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
player=newPlayer;
[player prepareToPlay];
[player play];
NSLog(@"摇到 xxx");
}
}
、、、

相关文章

  • 小细节大体验-微信摇一摇

    微信摇一摇-性别为女时 微信摇一摇-性别为男时

  • 微信摇奖盒为什么能有效提高商家的营销效果

    理解微信摇奖盒概念的前提 很多人用过微信的“摇一摇”功能,微信的这个“摇一摇”,可谓是神通广大。其中,“摇人”功能...

  • 微信摇一摇

    最近有做到摇一摇, 发现还是很简单的, 随手一记主要实现的功能是: 开始要摇动的时候, 图片会向两边分开, 并且会...

  • 解密iBeacon物联网和火到爆的摇一摇

    春晚的微信摇一摇抢红包,让摇一摇功能又火了一把。如今微信开放的摇一摇周边接口可能在未来的很多年会一直融入到我们的日...

  • 朋友不在朋友圈

    微信,是一个伟大的发明。 记得从2013年开始玩微信,那时候的微信只是好奇里边的摇一摇和漂流瓶功能。那时候摇一摇能...

  • Android微信摇一摇

    1.在onStart() 方法中获取传感器的SensorManager 2.紧接着我们就要在Stop中注销传感器 ...

  • boy(4)

    后来我们用上了微信 那时候不是很火什么摇一摇啊什么功能吗 他就让我开微信和他一起摇一摇 看看能不能同时摇到对方 我...

  • iOS摇一摇功能实现

    看到微信的摇一摇功能是不是感觉很神奇呢?其实在iOS里想要实现摇一摇功能很简单,方法如下: 先在targets -...

  • iOS 微信摇一摇的实现

    微信的摇一摇是怎么实现的,在 UIResponder中存在这么一套方法 //这就是执行摇一摇的方法。那么怎么用这些...

  • 微信终于承认:“摇一摇”是“少儿不宜”

    微信在今日正式上线了青少年模式,开启后“摇一摇”、“附近的人”将不可访问,有网友戏称:“你终于承认你家摇一摇是少儿...

网友评论

      本文标题:微信摇一摇

      本文链接:https://www.haomeiwen.com/subject/fmagkttx.html