How can I add gravity to UIButton SWIFT - swift

So I am trying to add gravity to 4 UIButtons but they are not falling, I added this code to the view did load, I don't why this isn't working well.
Here is the code:
//initialize the animator
var animator = UIDynamicAnimator(referenceView: self.view)
//add gravity
let gravity = UIGravityBehavior(items: [redButton!, greenButton!, blueButton!, cameraButton!])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
animator.addBehavior(gravity)
My buttons are the redButton, greenButton, blueButton, and the cameraButton, also I have applied the direction of the fall but when I run the app they are just static. So is it possible to add gravity to the UIButtons?

You are declaring your animator locally to the function. When the function returns, the animator is released from memory, and all animation stops (actually viewDidLoad() returns so early in the lifecycle that animation never gets started).
Declare it as:
var animator: UIDynamicAnimator!
at class scope
Class scope means put it within the curly braces of the class:
class Foo {
var thisVariableIsAtClassScopeAndPersistsAcrossMethods: Int
func bar() {
var thisVariableIsAtLocalScopeAndDisappearsWhenFunctionReturns: String
}
}
And only initialize it in viewDidLoad():
animator = UIDynamicAnimator(referenceView: self.view)
(You cannot initialize it at class scope without lazy trickiness, because self is not yet available.)
** EDIT FINAL ANSWER **
After chat, we resolved the misunderstood answer and now we have:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var redButton: UIButton!
#IBOutlet weak var greenButton: UIButton!
#IBOutlet weak var blueButton: UIButton!
#IBOutlet weak var cameraButton: UIButton!
// Declare the animator at class scope so it doesn't get released prematurely.
var animator: UIDynamicAnimator!
override func viewDidLoad() {
super.viewDidLoad()
//add gravity
let gravity = UIGravityBehavior(items: [redButton, greenButton, blueButton, cameraButton])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
animator.addBehavior(gravity)
// other viewDidLoad setup code to set attributes of buttons
}
}

Related

Anchoring Multiple Scenes in RealityKit

While loading multiple scenes (from reality composer) into arView, the scenes is not anchored in the same space.
In this example, scene1 is loaded when the app starts. After the button is pressed, the scene2 is added into the scene. In both the scenes, the models are placed at the origin and are expected to overlap with scene2 is added into the view. However, the position of scene1 and scene2 is different when they are added into the arView.
import UIKit
import RealityKit
class ViewController: UIViewController {
#IBOutlet var arView: ARView!
#IBOutlet weak var button: UIButton!
var scene1: Experience.Scene1!
var scene2: Experience.Scene2!
override func viewDidLoad() {
super.viewDidLoad()
// Load the "Box" scene from the "Experience" Reality File
scene1 = try! Experience.loadScene1()
scene2 = try! Experience.loadScene2()
// Add the box anchor to the scene
arView.scene.addAnchor(scene1)
}
#IBAction func buttonPressed(_ sender: Any) {
arView.scene.addAnchor(scene2)
}
}
Note: This issues does not happen when both the scenes are added simultaneously.
How to make sure that both the scenes are anchored at the same ARAnchor?
Use the following approach:
let scene01 = try! Cube.loadCube()
let scene02 = try! Ball.loadSphere()
let cubeEntity: Entity = scene01.steelCube!.children[0]
let ballEntity: Entity = scene02.glassBall!.children[0]
// var cubeComponent: ModelComponent = cubeEntity.components[ModelComponent].self!
// var ballComponent: ModelComponent = ballEntity.components[ModelComponent].self!
let anchor = AnchorEntity()
anchor.addChild(cubeEntity)
anchor.addChild(ballEntity)
// scene01.steelCube!.components.set(cubeComponent)
// scene02.glassBall!.components.set(ballComponent)
arView.scene.anchors.append(anchor)

In Swift’s UIKit Dynamics, how can I define a circle boundary to contain a UIView?

I have researched a LOT, but the only examples I can find anywhere are for the purpose of defining the bounds of a UIView so that they collide/bounce off each other on the OUTSIDE of the objects.
Example: A ball hits another ball and they bounce away from each other.
But what I want to do is create a circular view to CONTAIN other UIViews, such that the containing boundary is a circle, not the default square. Is there a way to achieve this?
Yes, that's totally possible. The key to achieving collision within a circle is to
Set the boundary for the collision behaviour to be a circle path (custom UIBezierPath) and
Set the animator’s referenceView to be the circle view.
Output:
Storyboard setup:
Below is the code of the view controller for the above Storyboard. The magic happens in the simulateGravityAndCollision method:
Full Xcode project
class ViewController: UIViewController {
#IBOutlet weak var redCircle: UIView!
#IBOutlet weak var whiteSquare: UIView!
var animator:UIDynamicAnimator!
override func viewDidLoad() {
super.viewDidLoad()
self.redCircle.setCornerRadius(self.redCircle.bounds.width / 2)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) { [unowned self] in
self.simulateGravityAndCollision()
}
}
func simulateGravityAndCollision() {
//The dynamic animation happens only within the reference view, i.e., our red circle view
animator = UIDynamicAnimator.init(referenceView: self.redCircle)
//Only the inside white square will be affected by gravity
let gravityBehaviour = UIGravityBehavior.init(items: [self.whiteSquare])
//We also apply collision only to the white square
let collisionBehaviour = UICollisionBehavior.init(items:[self.whiteSquare])
//This is where we create the circle boundary from the redCircle view's bounds
collisionBehaviour.addBoundary(withIdentifier: "CircleBoundary" as NSCopying, for: UIBezierPath.init(ovalIn: self.redCircle.bounds))
animator.addBehavior(gravityBehaviour)
animator.addBehavior(collisionBehaviour)
}
}
extension UIView {
open override func awakeFromNib() {
super.awakeFromNib()
self.layer.allowsEdgeAntialiasing = true
}
func setCornerRadius(_ amount:CGFloat) {
self.layer.cornerRadius = amount
self.layer.masksToBounds = true
self.clipsToBounds = true
}
}

Swift delegation and storyboard #IBDesignable issue

Below is just a test of delegation.
What I did was, 1) draw a rectangle, 2) set this rectangle's width of line with a delegate, 3) Hope the storyboard could update its display.
There are two questions:
The first is: If I use "testView.widthdelegate = ViewController()" rather than "testView.widthdelegate = self" , the "var widthValue: CGFloat? = widthdelegate?.trueWidth" will be nil, but it should be 50, what's different between "self" and "ViewController()"?
The second is: I still want to update the result of draw in storyboard, where you can see I did a SetNeedDisplay() but no use at all, how could I do it?
View
import UIKit
protocol widthDelegate: class {
var trueWidth: CGFloat { get }
}
#IBDesignable
class TestView: UIView {
weak var widthdelegate: widthDelegate?
override func drawRect(rect: CGRect) {
var widthValue: CGFloat? = widthdelegate?.trueWidth ?? 1.0
rectangle(widthRefer: widthValue!)
println("width in TestView is \(widthdelegate?.trueWidth)" )
}
func rectangle(#widthRefer: CGFloat) -> UIBezierPath{
var rect = UIBezierPath(rect: CGRect(x: bounds.width/2-50, y: bounds.height/2-50, width: 100, height: 100))
rect.lineWidth = widthRefer
rect.stroke()
return rect
}
}
Controller
import UIKit
class ViewController: UIViewController,widthDelegate {
var trueWidth: CGFloat = 50
#IBOutlet var testView: TestView!{
didSet{ //after the storyboard loaded.
// testView.widthdelegate = ViewController()
testView.widthdelegate = self
testView.setNeedsDisplay()
}
}
}
Answer 1:
self is the actual instance of the class the code is in (correct solution)
ViewController() creates a brand new instance of ViewController which is not identical with the instance created in IB (wrong solution)
Answer 2:
Never implement didSet for an IBOutlet because it's never called during initialization. Better use viewDidLoad() for settings
Some other notes:
Please consider the naming convention that class, protocol and enum names start with a capital letter.
The class constraint in the protocol declaration is not needed

How do I change the CGRect to buttons?

I originally used rectangles to test out the program but now I need to make them buttons. How would I do that programmatically?
import UIKit
class interestViewController: UIViewController {
var squareView: UIView!
var square: UIView!
var frogs: UIView!
var colson: UIView!
var wwdc: UIView!
var gravity: UIGravityBehavior!
var animator: UIDynamicAnimator!
var collision: UICollisionBehavior!
override func viewDidLoad() {
super.viewDidLoad()
squareView = UIView(frame: CGRectMake(100, 100, 100, 100))
view.addSubview(squareView)
animator = UIDynamicAnimator(referenceView: view)
squareView.backgroundColor = UIColor.blackColor()
gravity = UIGravityBehavior(items: [squareView])
animator.addBehavior(gravity)
collision = UICollisionBehavior(items: [squareView])
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collision)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Question: How would I do that programmatically?
Short answer: Don't. Create your buttons in IB. It's easier than code the first time, and way, waaaaay easier to update and maintain. Learn to use IB. It's a huge timesaver.
If you are bound and determined to do things the hard way, then you'll need to use one of the UIButton class initializers, like buttonWithType. Then you'd set the frame and other attributes like you're doing with your squareView.

UIKit Collision Detection and actions

I am developing an iOS app with swift and i ran into a problem. Using solely UIKit, I have a square that falls onto a platform, bounces back to its original position, and keeps going. I want the platform to change color every time the square hits it but i don't know how to detect that the square has come into contact with the platform. Please help. Here is the the code
import UIKit
import SpriteKit
class PlayScreen : UIViewController {
#IBOutlet var ScreenBack: UIImageView!
#IBOutlet var Platform: UIImageView!
var squareView: UIImageView!
var gravity: UIGravityBehavior!
var animator: UIDynamicAnimator!
var collision: UICollisionBehavior!
var itemBehaviour: UIDynamicItemBehavior!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
// Creating a bouncing Ball with the barrier of the platform
squareView = UIImageView(frame: CGRect(x: ScreenBack.frame.width/2, y: ScreenBack.frame.height/4, width: 40, height: 40))
squareView.frame.origin.x-=squareView.frame.width/2
squareView.image = redBall
view.addSubview(squareView)
animator = UIDynamicAnimator(referenceView: view)
gravity = UIGravityBehavior(items: [squareView])
animator.addBehavior(gravity)
collision = UICollisionBehavior(items: [squareView])
collision.translatesReferenceBoundsIntoBoundary = true
collision.addBoundaryWithIdentifier("barrier", fromPoint: CGPointMake(self.Platform.frame.origin.x, self.Platform.frame.origin.y), toPoint: CGPointMake(self.Platform.frame.origin.x + self.Platform.frame.width, Platform.frame.origin.y))
animator.addBehavior(collision)
itemBehaviour = UIDynamicItemBehavior(items: [squareView])
itemBehaviour.elasticity = 1.0
itemBehaviour.resistance = 0.0
animator.addBehavior(itemBehaviour)
}
}
to detect collision use UICollisionBehaviorDelegate
class ViewController: UIViewController, UICollisionBehaviorDelegate {
Set your collision behavior object delegate to self
collision.collisionDelegate=self
Add add the following function
func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying, atPoint p: CGPoint) {
println("Contact - \(identifier)")
}