2017-03-26 8 views
-2

シェイクジェスチャーを行う方法と場所を個別に見つける方法はわかっていますが、ユーザーがデバイスを揺すると場所を見つける方法はわかりません。私はxcodeバージョン6.4を持っています。スウィフト - シェイクモーションが終了した後に場所を見つける

私はモーション終了機能を持っています。そして、showUserLocation関数。
私はshowUserLocationを呼び出すmotionEndedにステートメントを入れて、ユーザーが電話を揺するとその位置が見つかるようにしたいと思います。

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { 

     ????? 

    } 

// Showing the user location 
func showUserLocation(sender : AnyObject) { 
    let status = CLLocationManager.authorizationStatus() 

    //Asking for authorization to display current location 
    if status == CLAuthorizationStatus.NotDetermined { 
     locationManager.requestWhenInUseAuthorization() 
    } else { 
     locationManager.startUpdatingLocation() 
    } 
+0

この質問が広すぎると、答えは簡単な答えであるかの電話シェイクした後、あなたのコード内で、あなたは揺れが起こっている知っている、ほとんど常にソフトウェア開発の場合のように実装するのは難しいかもしれ現在位置を見つける –

+0

より具体的には、これらの括弧内には何が入りますか func motionEnded(motion:UIEventSubtype、withEventイベント:UIEvent){ ???? } 私が設定したshowUserLocation関数に接続します –

+0

あなたはコメントの投稿コードが誰にも分かりにくく答えを与えるような質問はできません。ブラケットに入るのはあなたが望むコードですスコープ内で、正しいスレッドでアクセスできる場合は、あなたの関数を呼び出すことができます –

答えて

0

あなたの質問は、手ぶれの終了または呼び出し場所とは関係ありません。あなたの質問は、Swiftの基本的な構文の1つであり、プログラミング言語の一般的な理解です。

場所を取得するためのコード実装は、アクションファンクションに「ボックス化」されました。おそらく、送信者が引数として必要だったUIButtonのタッチアップです。しかし、位置情報を取得するために送信者をまったく使用しないので、この実装を独自の関数showUserLocationHelperに移動するのは簡単です。したがって、アクションfuncはgetLocationを呼び出すことができ、shakeモーションはget locationを呼び出すことができます。

あなたのアクション関数の名前

showUserLocationは不十分長い名前、それは btnShowUserLocationTouchUpInsideのような名前を持つべきという名前がありますが、あなたのアクション機能という名前のもののように混乱していないとよりは motionEndedに使用しようとしています。

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { 
    //you can't pass the sender so couldn't call your showUserLocation action function, 
    //so move implementation of showUserLocation to showUserLocationHelper and now you can do what you need after shake event 
    showUserLocationHelper() 
} 

// Showing the user location 
//this is an action function for some UI element like a UIButton and the sender is required to be passed for this function to fire thus, motionEnded cannot directly call it 
//but you do not use sender at all 
func showUserLocation(sender : AnyObject) { 
    showUserLocationHelper() 
} 

func showUserLocationHelper() { 
    let status = CLLocationManager.authorizationStatus() 

    //Asking for authorization to display current location 
    if status == CLAuthorizationStatus.NotDetermined { 
     locationManager.requestWhenInUseAuthorization() 
    } else { 
     locationManager.startUpdatingLocation() 
    } 
} 
関連する問題