IOS shake function, ios Function
The effect is as follows:
Related Materials
The related code is as follows:
# Import "ShakeViewController. h "# import <AudioToolbox/AudioToolbox. h> # import <AVFoundation/AVFoundation. h> # define kScreenWidth [UIScreen mainScreen]. bounds. size. width # define kScreenHeight [UIScreen mainScreen]. bounds. size. height @ interface ShakeViewController () @ property (nonatomic, strong) UIImageView * topImgView; @ property (nonatomic, strong) UIImageView * botImgView; @ property (nonatomic) SystemSoundID soundID; @ end @ implementation ShakeViewController-(void) viewDidLoad {[super viewDidLoad]; [UIApplication sharedApplication]. applicationSupportsShakeToEdit = YES; [self becomeFirstResponder]; self. view. backgroundColor = [UIColor colorwithred: 245/255. 0 green: 245/255. 0 blue: 245/255. 0 alpha: 1.0]; [self inintImageView]; [self initSoudID];} // initialize a vibrating image-(void) ininintimageview {// center position CGFloat y = 64 + (kScreenHeight-64)/2; // ratio width/height CGFloat scale = 208/320. 0; _ topImgView = [[UIImageView alloc] initWithFrame: CGRectMake (0, y-(kScreenWidth * scale), kScreenWidth, kScreenWidth * scale)]; _ topImgView. image = [UIImage imageNamed: @ "Shake_01"]; [self. view addSubview: _ topImgView]; _ botImgView = [[UIImageView alloc] initWithFrame: CGRectMake (0, y, kScreenWidth, kScreenWidth * scale)]; _ botImgView. image = [UIImage imageNamed: @ "Shake_02"]; [self. view addSubview: _ botImgView];} // assign a value to soundID-(void) initSoudID {NSString * path = [[NSBundle mainBundle] pathForResource: @ "shake_sound_male" ofType: @ "wav"]; AudioServicesCreateSystemSoundID (_ bridge CFURLRef) [NSURL fileURLWithPath: path], & _ soundID );} ********** * *******************/-(void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event {// play AudioServicesPlaySystemSound (_ soundID); CGFloat y_top = _ topImgView. frame. origin. y; CGFloat y_bot = _ botImgView. frame. origin. y; [UIView animateWithDuration: 0.3 animations: ^ {_ topImgView. frame = CGRectMake (0, y_top-50, _ topImgView. frame. size. width, _ topImgView. frame. size. height); _ botImgView. frame = CGRectMake (0, y_bot + 50, _ botImgView. frame. size. width, _ botImgView. frame. size. height);} completion: ^ (BOOL finished) {[UIView animateWithDuration: 0.3 animations: ^ {_ topImgView. frame = CGRectMake (0, y_top, _ topImgView. frame. size. width, _ topImgView. frame. size. height); _ botImgView. frame = CGRectMake (0, y_bot, _ botImgView. frame. size. width, _ botImgView. frame. size. height);} completion: ^ (BOOL finished) {}];}-(void) motionEnded :( UIEventSubtype) motion withEvent :( UIEvent *) event {[self defined mselector: @ selector (vibrate) withObject: nil afterDelay: 0.6];}-(void) vibrate {// vibration AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);} @ end