ios 摇一摇 仿微信

来源:互联网 发布:做淘宝直播怎么赚钱 编辑:程序博客网 时间:2024/05/18 02:36
微信的摇一摇是怎么实现的~发现原来 ios本身就支持
在 UIResponder中存在这么一套方法
 
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 
这就是执行摇一摇的方法。那么怎么用这些方法呢?
 
很简单,你只需要让这个Controller本身支持摇动
 
同时让他成为第一相应者:
 
- (void)viewDidLoad
 
{
 
    [superviewDidLoad];
 
// Do any additional setup after loading the view, typically from a nib.
 
    [[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];
 
    [selfbecomeFirstResponder];
 
}
 
  
 
然后去实现那几个方法就可以了
 
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
 
{
 
    //检测到摇动
 
}
 
  
 
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
 
{
 
    //摇动取消
 
}
 
  
 
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
 
{
 
    //摇动结束
 
    if (event.subtype == UIEventSubtypeMotionShake) {
 
        //something happens
 
    }
 
}


IOS实现摇一摇源代码


  1. @interface ShakeViewController : UIViewController<UIAccelerometerDelegate>  
  2. {  
  3.     UIAccelerationValue    myAccelerometer[3];  
  4.   
  5.     //是否响应摇一摇的标志  
  6.     BOOL  _canShake;  
  7.       
  8. }  

.m文件中

  1. #define kFilteringFactor                0.1  
  2. #define kEraseAccelerationThreshold        2.0  
  3.   
  4. @implementation ShakeViewController  
  5. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  6. {  
  7.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  8.     if (self) {  
  9.         _canShake = YES;  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. - (void)dealloc  
  15. {  
  16.     [UIAccelerometer sharedAccelerometer].delegate = nil;  
  17.     [super dealloc];  
  18. }  
  19.   
  20. - (void)viewDidLoad  
  21. {  
  22.     [super viewDidLoad];  
  23.     [UIAccelerometer sharedAccelerometer].delegate = self;  
  24.     [UIAccelerometer sharedAccelerometer].updateInterval = 1.0f/40.0f;  
  25. }  
  26.   
  27.   
  28. #pragma mark - UIAccelerometerDelegate  
  29. - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration  
  30. {  
  31. UIAccelerationValue  length, x, y, z;  
  32.       
  33.     if (!_canShake)  
  34.     {  
  35.         return;  
  36.     }  
  37.       
  38.     //Use a basic high-pass filter to remove the influence of the gravity  
  39.     myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);  
  40.     myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);  
  41.     myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);  
  42.     // Compute values for the three axes of the acceleromater  
  43.     x = acceleration.x - myAccelerometer[0];  
  44.     y = acceleration.y - myAccelerometer[0];  
  45.     z = acceleration.z - myAccelerometer[0];  
  46.       
  47.     //Compute the intensity of the current acceleration  
  48.     length = sqrt(x * x + y * y + z * z);  
  49.     // If above a given threshold, play the erase sounds and erase the drawing view  
  50.     if(length >= kEraseAccelerationThreshold)  
  51.     {  
  52.         //是否响应摇一摇的标志  
  53.         _canShake = NO;  
  54.         [self shakeEvent];  
  55.     }  
  56. }  



1。 在App's Delegate中设定applicationSupportsShakeToEdit属性:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

    

    application.applicationSupportsShakeToEditYES; //在ios6.0后,这里其实都可以不写了

    

    self.window= [[UIWindowallocinitWithFrame:[[UIScreenmainScreenbounds]];

    // Override point for customization after application launch.

    self.viewController= [[ViewControllerallocinitWithNibName:@"ViewController"bundle:nil];

    self.window.rootViewControllerself.viewController;

    [self.windowmakeKeyAndVisible];

    returnYES;

}


2。在你的View控制器中添加/重载canBecomeFirstResponderviewDidAppear:以及viewWillDisappear:


//这里很重要,因为大部分视图 默认 的  canBecomeFirstResponder 是 NO的

-(BOOL)canBecomeFirstResponder {

    return YES;

}


-(void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    [self becomeFirstResponder];

}


-(void)viewWillDisappear:(BOOL)animated {

    [self resignFirstResponder];

    [super viewWillDisappear:animated];

}


3。在你的view控制器中添加motionEnded

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event

{

    if(motion == UIEventSubtypeMotionShake)

    {

        // your code

    }

}


---------------------------------------------------------------

IOS 3.0 + 开始支持motion事件,检测设备摇动 

 – motionBegan:withEvent:       摇动开始时执行 

 – motionEnded:withEvent:       摇动结束时执行

 – motionCancelled:withEvent:  摇动被取消时执行  

0 0
原创粉丝点击