亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當有人搖動iPhone時,我該如何檢測?

當有人搖動iPhone時,我該如何檢測?

iOS
交互式愛情 2019-06-26 15:06:38
當有人搖動iPhone時,我該如何檢測?我想在有人搖晃iPhone的時候做出反應。我不在乎他們是如何搖動它的,只是它被有力地揮動了一下。有人知道怎么發現這個嗎?
查看完整描述

3 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

在3.0,現在有一個更容易的方法-鉤到新的運動事件。

主要的訣竅是,您需要一些UIView(而不是UIViewController)作為FirstResponder來接收抖動事件消息。下面是您可以在任何UIView中使用的代碼來獲得抖動事件:

@implementation ShakingView- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        // Put in code here to handle shake
    }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];}- (BOOL)canBecomeFirstResponder{ return YES; }@end

您可以輕松地將任何UIView(甚至系統視圖)轉換為一個視圖,只需使用這些方法對視圖進行子類(然后選擇此新類型而不是IB中的基類型,或者在分配視圖時使用它),就可以簡單地獲得抖動事件。

在視圖控制器中,您希望將此視圖設置為第一個響應者:

- (void) viewWillAppear:(BOOL)animated{
    [shakeView becomeFirstResponder];
    [super viewWillAppear:animated];}- (void) viewWillDisappear:(BOOL)animated{
    [shakeView resignFirstResponder];
    [super viewWillDisappear:animated];}

不要忘記,如果您有其他視圖成為用戶操作的第一個響應者(如搜索欄或文本輸入字段),則還需要在其他視圖退出時恢復抖動視圖第一響應狀態!

即使將application ationSupportsShakeToEdit設置為no,此方法也有效。


查看完整回答
反對 回復 2019-06-26
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

從我的狄沙克申請:

// Ensures the shake is strong enough on at least two axes before declaring it a shake.// "Strong enough" means "greater than a client-supplied threshold" in G's.static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
    double
        deltaX = fabs(last.x - current.x),
        deltaY = fabs(last.y - current.y),
        deltaZ = fabs(last.z - current.z);

    return
        (deltaX > threshold && deltaY > threshold) ||
        (deltaX > threshold && deltaZ > threshold) ||
        (deltaY > threshold && deltaZ > threshold);}@interface L0AppDelegate : NSObject <UIApplicationDelegate> {
    BOOL histeresisExcited;
    UIAcceleration* lastAcceleration;}@property(retain) UIAcceleration* lastAcceleration;@end@implementation L0AppDelegate- (void)applicationDidFinishLaunching:(UIApplication *)application {
    [UIAccelerometer sharedAccelerometer].delegate = self;}- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

    if (self.lastAcceleration) {
        if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
            histeresisExcited = YES;

            /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */

        } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
            histeresisExcited = NO;
        }
    }

    self.lastAcceleration = acceleration;}// and proper @synthesize and -dealloc boilerplate code@end

計量學防止抖動事件多次觸發,直到用戶停止抖動。


查看完整回答
反對 回復 2019-06-26
  • 3 回答
  • 0 關注
  • 599 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號