iOS 搖一搖的功能,ios功能

來源:互聯網
上載者:User

iOS 搖一搖的功能,ios功能

效果如下

相關素材

 

相關代碼如下

#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];}//初始化震動的圖片- (void)inintImageView{    //中間線位置    CGFloat y = 64+(kScreenHeight-64)/2;    //圖片的比例  寬/高    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];}//對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{    //播放    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 performSelector:@selector(vibrate) withObject:nil afterDelay:0.6];}-(void)vibrate{    //震動    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);}@end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.