我正在做一个应用程序,在不同的子视图中显示4个圆圈,当你点击一个圆圈时,它会变大一点然后如果你点击另一个圆圈,最后一个回到它的初始大小,新的一个变得更大等...
这是函数的代码:
func selectCircle(sender: UIButton) {
print("clicked")
if ((lastViewSelected) != nil)
{
print("lastViewSelected was not null")
if (lastViewSelected != sender.superview)
{
print("you clicked another circle")
UIView.animateWithDuration(0.5, animations: {
self.lastViewSelected!.transform = CGAffineTransformMakeScale(1, 1)
})
UIView.animateWithDuration(0.5, animations: {
sender.superview!.transform = CGAffineTransformMakeScale(1.2, 1.2)
})
}
else
{
print("you clicked the same circle")
UIView.animateWithDuration(0.5, animations: {
self.lastViewSelected!.transform = CGAffineTransformMakeScale(1, 1)
})
lastViewSelected = nil
}
}
else
{
print("lastViewSelected was so null 0 was jealous")
UIView.animateWithDuration(0.5, animations: {
sender.superview!.transform = CGAffineTransformMakeScale(1.2, 1.2)
})
}
lastViewSelected = sender.superview
}
当你在一个圆圈上点击两次(为了使它更大然后再回到它的大小)然后你再也不能点击这个,因为我检查用户是否点击相同但是该功能不知道圆圈的状态
这就是为什么在你第二次点击同一个圆圈之后我想把lastViewSelected设置为nil导致它回到零状态,就像没有被点击过一样。
但显然它没有发生。当我再次点击一个被点击并重新点击的圆圈时,程序仍然进入lastViewSelected!= nil条件
我知道我可以使用布尔来处理它,但我只是想知道
为什么lastViewSelected = nil
指令不起作用?
它是否与之前运行的动画有关?
我真的希望整个解释清楚
答案 0 :(得分:3)
您已将其分配给nil
,然后在功能结束时再次使用lastViewSelected = sender.superview
分配该功能,您需要在分配之前退回该功能试。
...
print("you clicked the same circle")
UIView.animateWithDuration(0.5, animations: {
self.lastViewSelected!.transform = CGAffineTransformMakeScale(1, 1)
})
lastViewSelected = nil
return
...
答案 1 :(得分:1)
只需更换行
即可lastViewSelected = nil
带行
{_ in self.lastViewSelected = nil}
这是动画的完成处理程序。 快乐的编码!