我想要使用locationInView来获取触摸的位置。当我将locationInView打印到控制台时,会输出正确的值。但是,当我尝试并将其分配给本地变量时,该变量永远不会获取该值,因此我的代码的其余部分不起作用。另外,这是UIControl的一个子类。touch.locationInView不分配给本地变量
以下摘录:
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool
{
NSLog("\(touch.locationInView(self))") // Prints out the correct location
let newPoint = touch.locationInView(self) // Never gets assigned to newPoint
var calculatedIndex: Int?
for (index, item) in labels.enumerate() {
if item.bounds.contains(newPoint) {
calculatedIndex = index
}
}
if calculatedIndex != nil {
selectedIndex = calculatedIndex!
sendActionsForControlEvents(.ValueChanged)
}
return false
}
我怀疑实际发生了什么,记录/打印'newPoint'的输出是什么? – luk2302
我添加了Xcode和调试器的本地和控制台的快照,显示发生了什么。 – bryken
Xcode调试器已知有相当多的错误,请在分配newPoint日志newPoint后添加NSLog语句。 – luk2302