Group Group Group Group Group Group Group Group Group Forums

UIKit Dynamics Tutorial: Getting Started

Learn how to make your user interfaces in iOS feel realistic with this UIKit Dynamics tutorial, updated for Swift!

This is a companion discussion topic for the original entry at

after i signed following function

i don’t seed any think in the log

func collisionBehavior(behavior: UICollisionBehavior!, beganContactForItem item: UIDynamicItem!, withBoundaryIdentifier identifier: NSCopying!, atPoint p: CGPoint) {
println(“Boundary contact occurred - (identifier)”)

this is example from the project:==>>
import UIKit

protocol UIDynamicItem: NSObjectProtocol{
var center: CGPoint{get set}
var bounds: CGRect{get}
var transform: CGAffineTransform{get set}


class ViewController: UIViewController, UICollisionBehaviorDelegate {

var animator: UIDynamicAnimator!
var gravity: UIGravityBehavior!
var collision: UICollisionBehavior!

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.
    let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
    square.backgroundColor = UIColor.grayColor()
    let barier = UIView(frame: CGRect(x: 0, y: 300, width: 130, height: 20))
    barier.backgroundColor = UIColor.redColor()
    animator = UIDynamicAnimator(referenceView: view)
    gravity = UIGravityBehavior(items: [square])
    collision = UICollisionBehavior(items: [square])
    collision.collisionDelegate = self
    //collision = UICollisionBehavior(items: [square, barier])
    collision.addBoundaryWithIdentifier("barier", forPath: UIBezierPath(rect: barier.frame))
    collision.translatesReferenceBoundsIntoBoundary = true

// collision.action = {
// print("(NSStringFromCGAffineTransform(square.transform)) (NSStringFromCGPoint(")
// }

func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, atPoint p: CGPoint) {
    print("Boundary connect acured - \(identifier)")

override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.


Just ran through this and there are minor syntax issues with Swift 3 in Xcode 8.3.3, so I’ve uploaded my version of the resulting code after following the tutorial:

UIKit Dynamics Tutorial, updated for Swift 3 (Xcode 8.3.3)

This tutorial is more than six months old, so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]