kodeco.com 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 https://www.raywenderlich.com/2326-uikit-dynamics-tutorial-getting-started

Hi.
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() {
    super.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()
    view.addSubview(square)
    
    let barier = UIView(frame: CGRect(x: 0, y: 300, width: 130, height: 20))
    barier.backgroundColor = UIColor.redColor()
    view.addSubview(barier)
    
    animator = UIDynamicAnimator(referenceView: view)
    gravity = UIGravityBehavior(items: [square])
    animator.addBehavior(gravity)
    
    collision = UICollisionBehavior(items: [square])
    collision.collisionDelegate = self
    //collision = UICollisionBehavior(items: [square, barier])
    collision.addBoundaryWithIdentifier("barier", forPath: UIBezierPath(rect: barier.frame))
    collision.translatesReferenceBoundsIntoBoundary = true
    animator.addBehavior(collision)

// collision.action = {
// print(“(NSStringFromCGAffineTransform(square.transform)) (NSStringFromCGPoint(square.center))”)
// }

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

override func didReceiveMemoryWarning() {
    super.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! :]