zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Android 主题theme说明 摘记
    Android开发 去掉标题栏方法 摘记
    安卓项目五子棋代码详解(二)
    关于 ake sure class name exists, is public, and has an empty constructor that is public
    百度地图3.0实现图文并茂的覆盖物
    android onSaveInstanceState()及其配对方法。
    关于集成科大讯飞语音识别的 一个问题总结
    android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结
    C# 解析 json Newtonsoft果然强大,代码写的真好
    c#数据类型 与sql的对应关系 以及 取值范围
  • 原文地址:https://www.cnblogs.com/qianLL/p/5579416.html
Copyright © 2011-2022 走看看