IOS shake function, ios Function

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.