返回首页
当前位置: 主页 > 手机mobile >

iphone shake 探測

时间:2010-07-27 12:00来源:未知 作者:sam.huang 点击:
函數判斷是否shake了. staticBOOLL0AccelerationIsShaking(UIAcceleration*last,UIAcceleration*current,doublethreshold){ double deltaX = fabs (last.x-current.x), deltaY = fabs (last.y-current.y), deltaZ = fabs (last.z-current.z); return (
  

  函數判斷是否shake了.

  1. static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) { 
  2.     double 
  3.     deltaX = fabs(last.x - current.x), 
  4.     deltaY = fabs(last.y - current.y), 
  5.     deltaZ = fabs(last.z - current.z); 
  6.      
  7.     return 
  8.     (deltaX > threshold && deltaY > threshold) || 
  9.     (deltaX > threshold && deltaZ > threshold) || 
  10.     (deltaY > threshold && deltaZ > threshold); 

在shake探測對象中實現delegate方法

  1. //mark accelerometer delegate 
  2. - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { 
  3.      
  4.     if (self.lastAcceleration) { 
  5. /*histeresisExcited 表示是否已經在shake,避免過於頻繁的shake*/
  6.         if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) { 
  7.             histeresisExcited = YES
  8.             /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */ 
  9.         } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) { 
  10.             histeresisExcited = NO
  11.         } 
  12.     } 
  13.     self.lastAcceleration = acceleration

 

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
发布者资料
amw 查看详细资料 发送留言 加为好友 用户等级:高级会员 注册时间:2009-03-30 13:03 最后登录:2012-01-17 11:01
推荐内容