Add two pictures and Mav sound files
@interface Viewcontroller:uiviewcontroller
@property (weak,nonatomic) iboutlet Uiimageview *imageview;
@end
. m file
#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_ login, register @2x.png"];
Self.broken = [UIImage imagenamed:@ "Bg_ My Voucher _ expires _ Right @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
Use the accelerometer to shake and shake the animation effect