簡體   English   中英

iOS Swift中的碰撞檢測

[英]Collision detection in iOS Swift

我為三個按鈕和Floor UIImageView創建了一個碰撞檢測功能,但是在if !isRotating代碼行中出現“預期聲明”錯誤。 一點幫助將是巨大的。

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”檢查中。

我開始清理您的代碼以使其易於閱讀,並真的很難找到匹配的花括號。 然后我意識到,那是你的問題:)

您的viewDidLoad()方法從此處開始:

override func viewDidLoad() {
    super.viewDidLoad()

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

並在這里結束:

    animator!.addBehavior(collision)
}

之后,您將獲得以下代碼: if !isRotating { Xcode抱怨是因為您的代碼只是散布在類中,這是不允許的。 該代碼需要放入自己的方法中,例如func createSpinAnimation()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM