微信摇一摇

2016-03-01  本文已影响119人  YYYLynn

最近有做到摇一摇, 发现还是很简单的, 随手一记
主要实现的功能是: 开始要摇动的时候, 图片会向两边分开, 并且会有声音, 出现动画, 结束后, 图片合并, 动画消失,结束声音。
首先是系统框架: <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");
}
}
、、、

上一篇下一篇

猜你喜欢

热点阅读