【发布时间】:2017-07-27 20:09:56
【问题描述】:
试图获取代码,因此当if whiteDotDist < centerRadius - whiteDotRadius 被执行时,它下面的所有代码都处于活动状态,当它下面的代码被执行时,它再次变为非活动状态,直到再次执行if whiteDotDist < centerRadius - whiteDotRadius。有点像一个循环,所以你必须从中心到 smallDot 不断地往回走。很难用电脑解释。更新它给我错误'二进制运算符'
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
let centerRadius = 37.5
let whiteDotRadius = 23.5
let whiteDotDist = hypot(center.center.x - whiteDot.center.x, center.center.y - whiteDot.center.y - whiteDot.center.y)
if whiteDotDist < centerRadius - whiteDotRadius {
resetTimer() }
if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
addOne += 1
score.text = "\(addOne)"
resetTimer()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.startTimer), userInfo: nil, repeats: true)
smallDot.center = spawnRandomPosition()
}
}
}
【问题讨论】:
-
如果你知道两个圆的中心和半径,你可以使用
intersectingPointsOfCircles:这个要点中的函数:gist.github.com/akhilcb/8d03f1f88f87e996aec24748bdf0ce78。如果它在里面,它会将两个点都返回为零。框架包含不会为您处理边缘情况,其中小圆圈在内部但框架重叠。 -
这很有帮助,但并不能真正回答我的问题。我试图更新问题以更清楚感谢您的帮助!
-
inactive,你的意思是说你想让定时器失效?然后在 else 部分调用定时器对象的无效。它会停止。如果你想让我把这个作为答案,请告诉我。在 if 条件中添加 else 子句后,它应该可以工作。
-
我的意思是所有代码都不会在非活动状态下工作。除非whiteDot位于中心,否则这一切都不会起作用,然后它下面的代码将全部处于活动状态并正常工作,然后当'if(whiteDot.frame.contains(smallDot.frame)&& smallDot.image!= nil) " 执行后代码再次变为非活动状态,直到 whiteDot 居中,因此每次 whiteDot 居中时,它都会激活其下方的代码,因此每次都必须先居中然后 whiteDot。
-
是的,您应该将
else部分添加到if whiteDotDist < centerRadius - whiteDotRadius {条件,然后在那里使您的计时器无效。
标签: ios swift uiimageview uiimage uipangesturerecognizer