gpt4 book ai didi

ios - iOS Swift 中的碰撞检测

转载 作者:可可西里 更新时间:2023-11-01 00:52:03 25 4
gpt4 key购买 nike

我已经为三个按钮和 Floor UIImageView 创建了一个碰撞检测函数,但是我在 if !isRotating 代码行中收到“Expected Declaration”错误.一点帮助就太好了。

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
}

The error is in the "if isRotating" check.

最佳答案

我开始清理您的代码以使其更易于阅读,并且真的很难找到匹配的大括号。然后我意识到,那是你的问题:)

您的 viewDidLoad() 方法从这里开始:

override func viewDidLoad() {
super.viewDidLoad()

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

到此结束

    animator!.addBehavior(collision)
}

紧接着,您将获得以下代码:if !isRotating {。 Xcode 正在提示,因为您将这段代码散布在您的类中,这是不允许的。该代码需要进入它自己的方法,例如func createSpinAnimation()

关于ios - iOS Swift 中的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34341188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com