我在这里得到一个输入代码错误"实例成员'界限'不能用于类型' BallView' " 创建目标点变量时 我该如何解决这个问题?
import UIKit
import CoreMotion
class BallView: UIView {
var width = CGFloat(0)
var height = CGFloat(0)
var acceleration = CMAcceleration(x: 0, y: 0, z: 0)
private let image = UIImage(named : "ball")!
private let image1 = UIImage(named : "target")!
private let image2 = UIImage(named : "target")!
private let image3 = UIImage(named : "target")!
private let image4 = UIImage(named : "target")!
var currentPoint : CGPoint = CGPointZero {
didSet {
var newX = currentPoint.x
var newY = currentPoint.y
if newX < 0 {
newX = 0
ballXVelocity = 0
} else if newX > bounds.size.width - image.size.width {
newX = bounds.size.width - image.size.width
ballXVelocity = 0
}
if newY < 0 {
newY = 0
ballYVelocity = 0
} else if newY > bounds.size.height - image.size.height {
newY = bounds.size.height - image.size.height
ballYVelocity = 0
}
currentPoint = CGPointMake(newX, newY)
let currentRect = CGRectMake(newX, newY,
newX + image.size.width,
newY + image.size.height)
let prevRect = CGRectMake(oldValue.x, oldValue.y,
oldValue.x + image.size.width,
oldValue.y + image.size.height)
setNeedsDisplayInRect(CGRectUnion(currentRect, prevRect))
}
}
// set starting values for the ball's dynamic behavior:
private var ballXVelocity = 0.0 // the ball starts standing still
private var ballYVelocity = 0.0
private var lastUpdateTime = NSDate() // get the instant when we begin
var targetPoint : CGPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(bounds.size.width))),
CGFloat(arc4random_uniform(UInt32(bounds.size.height))))
var targetPoint1 : CGPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(bounds.size.width))),
CGFloat(arc4random_uniform(UInt32(bounds.size.height))))
var targetPoint2 : CGPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(bounds.size.width))),
CGFloat(arc4random_uniform(UInt32(bounds.size.height))))
var targetPoint3 : CGPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(bounds.size.width))),
CGFloat(arc4random_uniform(UInt32(bounds.size.height))))
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() -> Void {
width = bounds.size.width
height = bounds.size.height
currentPoint = CGPointMake((bounds.size.width / 2.0) +
(image.size.width / 2.0),
(bounds.size.height / 2.0) +
(image.size.height / 2.0))
}
override func drawRect(rect: CGRect) {
// Drawing code
image.drawAtPoint(currentPoint)
image1.drawAtPoint(targetPoint)
image2.drawAtPoint(targetPoint1)
image3.drawAtPoint(targetPoint2)
image4.drawAtPoint(targetPoint3)
}
func update() -> Void {
// what time is now, i.e. the current sampling instant:
let now = NSDate()
// how much time passed since the last sampling instant:
let secondsSinceLastDraw = now.timeIntervalSinceDate(lastUpdateTime)
// compute new velocity for the ball we draw on screen:
ballXVelocity =
ballXVelocity + (acceleration.x * secondsSinceLastDraw)
ballYVelocity =
ballYVelocity - (acceleration.y * secondsSinceLastDraw)
let xDelta = secondsSinceLastDraw * ballXVelocity * 1000
let yDelta = secondsSinceLastDraw * ballYVelocity * 1000
// change in position:
currentPoint = CGPointMake(currentPoint.x + CGFloat(xDelta),
currentPoint.y + CGFloat(yDelta))
// make the "last sampling instant" equal to now, to be used next time through:
lastUpdateTime = now
}
}