使用加速计实现摇一摇动画效果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用加速计实现摇一摇动画效果相关的知识,希望对你有一定的参考价值。

加入两张图片和mav声音的文件

@interface ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UIImageView *imageView;

@end

.m文件

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic,strong) UIImage *fixed;
@property (nonatomic,strong) UIImage *broken;
@property (nonatomic,assign) BOOL brokenScreenShowing;
@property (nonatomic,strong) AVAudioPlayer *crashPlayer;

@end

@implementation ViewController

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"SecondBeep" withExtension:@"wav"];
    
    NSError *error = nil;
    self.crashPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    if (!self.crashPlayer) {
        
        NSLog(@"Audio Error! %@",error.localizedDescription);
    }
    
    self.fixed = [UIImage imageNamed:@"bg_登录、注册@2x.png"];
    self.broken = [UIImage imageNamed:@"bg_我的代金券_过期_右@2x.png"];
    self.imageView.image = self.fixed;
    
 
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    if (!self.brokenScreenShowing && motion == UIEventSubtypeMotionShake) {
        self.imageView.image = self.broken;
        [self.crashPlayer play];
        self.brokenScreenShowing = YES;
    }


}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    self.imageView.image = self.fixed;
    self.brokenScreenShowing = NO;
}

@end

以上是关于使用加速计实现摇一摇动画效果的主要内容,如果未能解决你的问题,请参考以下文章

摇一摇的实现

利用传感器(sensor)实现微信摇一摇动画

android 动画, 请问这个动画怎么解决啊?哪位有思路啊?谢谢!

手机端 摇一摇功能 代码实现

安卓 摇一摇 包含 objectAnimator valueAnimator的动画效果

插值器(Interpolator)的使用说明