简体   繁体   中英

Collision detection in iOS Swift

I have created a collision detection function for three buttons and the Floor UIImageView , but I am getting a "Expected Declaration" error in the if !isRotating line of code. A little help would be great.

import UIKit

class ViewController: UIViewController {
    var location = CGPoint(x: 0, y: 0)
    var animator:  UIDynamicAnimator?
    var gravity: UIGravityBehavior?
    var isRotating = false

    var collision: UICollisionBehavior!

    @IBOutlet var Ball1: UIButton!
    @IBOutlet var Ball2: UIButton!
    @IBOutlet var Ball3: UIButton!
    @IBOutlet var Floor: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.animator = UIDynamicAnimator(referenceView: self.view)

        let gravity = UIGravityBehavior (items: [self.Ball1!, self.Ball2!, self.Ball3!])
        let direction = CGVectorMake(0.0, 1.0)
        gravity.gravityDirection = direction
        self.animator?.addBehavior(gravity)

        Ball1.center = CGPointMake(160, 330)
        Ball2.center = CGPointMake(39, 163)
        Ball3.center = CGPointMake(240, 74)

        collision = UICollisionBehavior(items: [Floor!, Ball1!, Ball2!, Ball3!])
        collision.translatesReferenceBoundsIntoBoundary = true
        collision.addBoundaryWithIdentifier("barrier", fromPoint: CGPointMake(self.view.frame.origin.x, 350), toPoint: CGPointMake(self.view.frame.origin.x + self.view.frame.width, 350))
        animator!.addBehavior(collision)
    }


    if !isRotating {
        // create a spin animation
        let spinAnimation = CABasicAnimation()
        // starts from 0
        spinAnimation.fromValue = 0
        // goes to 360 ( 2 * π )
        spinAnimation.toValue = M_PI*2
        // define how long it will take to complete a 360
        spinAnimation.duration = 1
        // make it spin infinitely
        spinAnimation.repeatCount = Float.infinity
        // do not remove when completed
        spinAnimation.removedOnCompletion = false
        // specify the fill mode
        spinAnimation.fillMode = kCAFillModeForwards
        // and the animation acceleration
        spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        // add the animation to the button layer
        Ball1.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")
        Ball2.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")
        Ball3.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")
    } else {
        // remove the animation
        Ball1.layer.removeAllAnimations()
        Ball2.layer.removeAllAnimations()
        Ball3.layer.removeAllAnimations()
    }

    // toggle its state
    isRotating = !isRotating
}

该错误在“ if isRotating”检查中。

I started cleaning up your code to make it easier to read, and really struggled to find a matching brace. Then I realised, that was your problem :)

Your viewDidLoad() method starts here:

override func viewDidLoad() {
    super.viewDidLoad()

    self.animator = UIDynamicAnimator(referenceView: self.view)

And ends here:

    animator!.addBehavior(collision)
}

Straight after that, you have this code: if !isRotating { . Xcode is complaining because you have this code just lying loose inside your class, which isn't allowed. That code needs to go inside a method of its own, eg func createSpinAnimation() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM