[Swift 1] Day 6-7 - App Challenge - Word Magnets


You just need to modify the Array, removing the item you don’t need and the re-do the line:
self.gravity = UIGravityBehavior(items: [arrayOfLabels])

You need to remove the item, first you need to find the index for the item, then remove the array item at that index.



Thanks. I must admit that I got no clue of accomplishing that.

The problem seems to be that the animator is added to the whole view and when I use self.animator.removeAllBehaviors(), it removes it from every item. I have tried to insert he panGesture.view but it does not take any arguments.

I also can’t replace the self.animator view panGesture.view (or label since label = panGesture.view as UILabel) since it will yield the error that the object has no animator…

Any hints would be greatly appreciated :smile:




Well, @Philomath, I haven’t used the Animator myself, so there might be better ways, but I would just wild guess and just suggest you create an Animator per label. Currently you are creating one global one and using it to animate all labels.
Then you can call it like : myLabel.animator!.removeAllBehaviours() ? I have to test it tbh…
I know you can also remove behaviours from the animator, if you have labeled them according to your labels or something else, you could say: animator.removeBehavior(label1)


@PaulSolt could you maybe advise on the best approach here?

Thanks in advance,




  1. You don’t want to create a new UIDynamicAnimator for each pan gesture action, that’s not good. Create it only in the viewDidLoad: method.
  2. You have a subtle issue on the sides. The pan gesture is letting the UILabel move past the bounding box. Again, this is something I’ll have to write something about. How to keep interactive content on screen with UIPanGestureRecognizers using the bounds (or other regions). Added to Trello: https://trello.com/c/22VahjHj

Two quick fixes that should work is to use the view’s bounds as the limits, currently you use a size that doesn’t work for all iOS devices (not dynamic).

Fix 1: Change this line in your method userGeneratedLabel

//  collision!.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsets(top: 0, left: 380, bottom: 0, right: 380))
    collision!.translatesReferenceBoundsIntoBoundary = true

Fix 2: Never let the position go outside the bounds by limiting when it is set, versus ignored.

 // Get the size of the iPhone/iPad screen
let screenSize = UIScreen.mainScreen().bounds

// Only set the new location when it is within bounds of 
//  screen (i.e. half visible because of center point of label)
if  location.x >= 0 &&
    location.x >= 0 &&
    location.x < CGRectGetMaxX(screenSize) &&
    location.y < CGRectGetMaxY(screenSize)

            label.center = location

  1. You have a label and String with the same name.

     var labelUG: String?

and in your method

   func userGeneratedLabel(labelText: String) {

Rename or remove one of them. It’s not doing what you think. The one in the method is masking the other, inside the method (scoping minor issue that can lead to bugs).

  1. I’ll have to put together a topic on best practices for both Gestures + UIKit Dynamics. Added to Trello: https://trello.com/c/22VahjHj
  2. I’m thinking through a guide on best practices for getting help faster, sorry for the delay. Added to Trello: https://trello.com/c/22VahjHj
  3. I’m not sure if you should recreate all the collisions each time. I think you just want to append one… not sure what you want.
  4. I would recommend formatting your code … I’ll have to do another post on code formatting and style. Use Control + i to reformat code. Added to Trello: https://trello.com/c/22VahjHj

@Philomath Have you looked at the documentation, guides, or other tutorials? If so, which ones?

Watch the WWDC videos on how to use UIKit Dynamics (google search is good too).

WWDC Videos

  1. Watch WWDC 2014 on UIKit Dynamics (beginner): https://developer.apple.com/videos/play/wwdc2013-206/
  2. Watch WWDC 2015 on new Cocoa Touch (fun things!): https://developer.apple.com/videos/play/wwdc2015-107/
  3. Watch WWDC 2015 on UIKit Dynamics (new): https://developer.apple.com/videos/play/wwdc2015-229/


  1. Download and play with Apple’s sample code on UIDynamics: https://developer.apple.com/library/ios/samplecode/DynamicsCatalog/Introduction/Intro.html
  2. UIKit Dynamics Swift Tutorial (2014 older + Objc, concepts remain the same): http://www.appcoda.com/intro-uikit-dynamics-tutorial/
  3. Swift UIKit Dynamics tutorial … not great code, may not be Swift 2.1: http://omarfouad.com/blog/2014/08/02/getting-started-uikitdynamics-swift/

Use ASCII WWDC to search for videos on topics.

Thanks for the help Vigo!

Attached a working solution: https://www.dropbox.com/s/my64v676jbjrbkw/Word%20Magnet%20-%20Physics%20UIKit%20Dynamics.zip?dl=0

There is still a lot of room for improvement… I’ll have to make sure there are better resources moving forward.