标签:
加入两张图片和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
标签:
原文地址:http://www.cnblogs.com/linxiu-0925/p/5511142.html