質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

299閲覧

Swift animationについて

Jose

総合スコア14

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/05/29 15:39

現在、スイフトの勉強をしております。
youtube等のコードを参考に、
画面に表示された四角を touchesBegan touchesMoved で任意の場所に動かし、
UIGravityBehaviorで重力を持つようにしました。

【改善した点】
touchesMovedで移動させた先で、四角が落下してしまうため、
タップ(クリック)している限りその場所に止まるようにし,
タップを離すと、落下するという動きが目標にしたいです。

追加すべきコードご教示いただけないでしょうか?

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 var location = CGPoint(x:0,y:0) 6 var animator: UIDynamicAnimator? 7 8 @IBOutlet weak var person: UILabel! 9 10 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 11 let touch: UITouch! = touches.first 12 location = touch.location(in: self.view) 13 person.center = location 14 } 15 16 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 17 let touch: UITouch! = touches.first 18 location = touch.location(in: self.view) 19 person.center = location 20 animator = UIDynamicAnimator(referenceView: self.view) 21 22 //add gravity 23 let gravity = UIGravityBehavior(items: [person]) 24 let direcction = CGVector(dx:0.0,dy:1.0) 25 gravity.gravityDirection = direcction 26 27 //collision 28 let boundries = UICollisionBehavior(items: [person]) 29 boundries.translatesReferenceBoundsIntoBoundary = true 30 31 //bounce 32 let bounce = UIDynamicItemBehavior(items: [person]) 33 bounce.elasticity = 0.5 34 35 animator?.addBehavior(bounce) 36 animator?.addBehavior(boundries) 37 animator?.addBehavior(gravity) 38 } 39 40 41 override func viewDidLoad() { 42 super.viewDidLoad() 43 44 //initialize the animator 45 animator = UIDynamicAnimator(referenceView: self.view) 46 47 //add gravity 48 let gravity = UIGravityBehavior(items: [person]) 49 let direcction = CGVector(dx:0.0,dy:1.0) 50 gravity.gravityDirection = direcction 51 52 //collision 53 let boundries = UICollisionBehavior(items: [person]) 54 boundries.translatesReferenceBoundsIntoBoundary = true 55 56 //bounce 57 let bounce = UIDynamicItemBehavior(items: [person]) 58 bounce.elasticity = 0.5 59 60 animator?.addBehavior(bounce) 61 animator?.addBehavior(boundries) 62 animator?.addBehavior(gravity) 63 person.center = CGPoint(x:160,y:330) 64 } 65 66 override func didReceiveMemoryWarning() { 67 super.didReceiveMemoryWarning() 68 // Dispose of any resources that can be recreated. 69 } 70 71 72} 73

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/05/29 20:05 編集

回答でなくて申し訳ないのですが、touchesEndedで処理してあげたらどうでしょうか?
guest

回答2

0

ベストアンサー

コードが全部載ってたので、自分のXcodeで試してみました。
とてもよくできてて、ほぼ理想の動きができてますね。

結論から言うと
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
を下記のもの
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
に書きかえれば、ほぼ理想としている状態になるのではないでしょうか?

なお、落下中に画面を押すと、一瞬だけ押した地点に現れて、
再び落下していたとこに戻るのは、touchesBeganなので、
押した瞬間だけ位置変更されるためです。
そこを修正したいなら、touchesBeganの最初に
animator?.removeAllBehaviors()
を追加すると、落下中に押した時、落下はなくなり、位置変更できるようになります。

これはJoseさんの理想かわかりませんが、最初の部分を下記のように修正した方が
触って動かせている感が出ると思います。touchesEndedには位置変更コードはいりません。

Swift

1 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 2 animator?.removeAllBehaviors() 3 } 4 5 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 6 let touch: UITouch! = touches.first 7 location = touch.location(in: self.view) 8 print(location) 9 person.center = location 10 }

さらにお節介かもしれませんが、

Swift

1 //add gravity 2 let gravity = UIGravityBehavior(items: [person]) 3 let direcction = CGVector(dx:0.0,dy:1.0) 4 gravity.gravityDirection = direcction 5 6 //collision 7 let boundries = UICollisionBehavior(items: [person]) 8 boundries.translatesReferenceBoundsIntoBoundary = true 9 10 //bounce 11 let bounce = UIDynamicItemBehavior(items: [person]) 12 bounce.elasticity = 0.5 13 14 animator?.addBehavior(bounce) 15 animator?.addBehavior(boundries) 16 animator?.addBehavior(gravity)

は、2回重複しているので、個別関数
例えば

Swift

1 func gravityOn(bool: bool) { 2 if bool == true { 3 //add gravity 4 let gravity = UIGravityBehavior(items: [person]) 5 let direcction = CGVector(dx:0.0,dy:1.0) 6 gravity.gravityDirection = direcction 7 8 //collision 9 let boundries = UICollisionBehavior(items: [person]) 10 boundries.translatesReferenceBoundsIntoBoundary = true 11 12 //bounce 13 let bounce = UIDynamicItemBehavior(items: [person]) 14 bounce.elasticity = 0.5 15 16 animator?.addBehavior(bounce) 17 animator?.addBehavior(boundries) 18 animator?.addBehavior(gravity) 19 } else { 20 animator?.removeAllBehaviors() 21 }

として、重力Onの時にgravityOn(bool:true)とすると見やすいかもしれないです。

投稿2018/05/29 20:56

編集2018/05/29 21:07
hameji

総合スコア1380

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hameji

2018/05/29 21:10 編集

重複してしまいました。すいません。 なので、追加でコードを色々足してみました。
guest

0

詳しくないので、とりあえず動くレベル。というか切り貼りしただけのものですが…。

swift

1override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 2 let touch: UITouch! = touches.first 3 location = touch.location(in: self.view) 4 person.center = location 5 animator = UIDynamicAnimator(referenceView: self.view) 6 } 7 8 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 9 let touch: UITouch! = touches.first 10 location = touch.location(in: self.view) 11 person.center = location 12 animator = UIDynamicAnimator(referenceView: self.view) 13 14 //add gravity 15 let gravity = UIGravityBehavior(items: [person]) 16 let direcction = CGVector(dx:0.0,dy:1.0) 17 gravity.gravityDirection = direcction 18 19 //collision 20 let boundries = UICollisionBehavior(items: [person]) 21 boundries.translatesReferenceBoundsIntoBoundary = true 22 23 //bounce 24 let bounce = UIDynamicItemBehavior(items: [person]) 25 bounce.elasticity = 0.5 26 27 animator?.addBehavior(bounce) 28 animator?.addBehavior(boundries) 29 animator?.addBehavior(gravity) 30 }

投稿2018/05/29 20:41

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Jose

2018/05/30 14:38

皆さまご回答ありがとうございました。 目的通りの動きができました。 引き続き開発進めたいと思います。
退会済みユーザー

退会済みユーザー

2018/05/30 15:28 編集

よかったです、がんばってください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問