![](/img/trans.png)
[英]Heavy initialization causes UIView animation to not animate at first try?
[英]UIView animation doesn't animate on the first try
我的問題是我的UIView動畫只能在第一次之后才能使用。 我有兩組不同的動畫。 第一組正確工作,第二組給我一些問題。 第一組將4個按鈕滑動到屏幕上,比我想要的位置更遠。 (這些位置稱為extendedState)第二組動畫將它們稍微滑回到我想要的位置。 這會產生彈跳回正確位置的效果。
在這兩個動畫之間有一小段時間,一旦第一組動畫完成,該按鈕將使按鈕閃爍回到其起始位置。 通過將起始位置設置為extendedStates(它們在停止動畫時所處的位置),這些按鈕似乎不會在動畫之間移動(這很好。這就是我想要的)。
操作順序如下:
這是按鈕處理程序的代碼:
@IBAction func selectAnimal(sender: UIButton) {
showButtons()
bigCircle.enabled = false
enableButtons()
if(tapToSelectLabel.hidden) {
animator.repositionButtonsToExtendedState(buttons) //sets the starting position to the extendedState so it appears not to move between animations
animator.slideButtonsIntoScreen(buttons, delegate: self) //this is the first set of animations
}
if(sender.titleLabel != nil) {
if(bigCircleLabel.text != "Choose an animal") {
if let answer:String = bigCircleLabel.text {
solution = game.checkAnswer(answer, question: game.threeQuestions[game.questionIndex])
}
showResponse()
}
}
}
現在這是animationDidStop塊中的代碼
let buttonRects:[CGRect] = [CGRectMake(834, 120, 175, 175),
CGRectMake(631, 198, 175, 175),
CGRectMake(470, 365, 175, 175),
CGRectMake(386, 578, 175, 175)]
UIView.animateWithDuration(0.35, delay: 0, options: [.BeginFromCurrentState, ], animations: {
self.buttons[3].frame = buttonRects[3]
}, completion: { (value:Bool) in
self.buttons[3].enabled = true
UIView.animateWithDuration(0.25, delay: 0, options: [ ], animations: {
self.buttons[2].frame = buttonRects[2]
}, completion: { (value:Bool) in
self.buttons[2].enabled = true
UIView.animateWithDuration(0.4, delay: 0, options: [ ], animations: {
self.buttons[1].frame = buttonRects[1]
}, completion: { (value:Bool) in
self.buttons[1].enabled = true
UIView.animateWithDuration(0.5, delay: 0, options: [ ], animations: {
self.buttons[0].frame = buttonRects[0]
}, completion: { (value:Bool) in
self.buttons[0].enabled = true
})
})
})
})
^這段代碼將按鈕稍微向后滑動到我希望它們進入的位置。
這是我的動畫師類的代碼
animator.slideButtonsIntoScreen
func slideButtonsIntoScreen(buttons:[UIButton], delegate:UIViewController) {
let center = CGPoint(x:delegate.view.frame.width, y:delegate.view.frame.height)
let startAngles:[CGFloat] = [0.405, 0.75, 1.1, 1.48]
let endAngles:[CGFloat] = [1.95, 2.25, 2.622, 2.98]
let radii:[CGFloat] = [555, 559, 558.5, 551]
let durations:[Double] = [1.75, 1.5, 1.25 , 1]
for index in 0...3 {
let path = UIBezierPath(arcCenter: center, radius: radii[index], startAngle: -startAngles[index], endAngle: -endAngles[index], clockwise: false)
let anim = CAKeyframeAnimation(keyPath: "position")
anim.path = path.CGPath
anim.rotationMode = kCAAlignmentNatural
anim.repeatCount = 0
anim.duration = durations[index] - 0.25
anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
anim.setValue("on", forKey: "type")
anim.delegate = delegate
buttons[index].layer.addAnimation(anim, forKey: "animate position along path"+String(index))
anim.removedOnCompletion = true
}
}
animator.repositionButtonsToExtendedState
func repositionButtonsToExtendedState(buttons:[UIButton]) {
let buttonExtendedRects:[CGRect] = [CGRectMake(755, 155, 175, 175),
CGRectMake(585, 245, 175, 175),
CGRectMake(450, 405, 175, 175),
CGRectMake(393, 590, 175, 175)]
for index in 0...3 {
buttons[index].frame = buttonExtendedRects[index]
}
}
我設置了斷點和打印語句,因此我知道它在第一次嘗試時就到達了動畫,只是它不顯示動畫。 每次第一次之后,它的工作原理完全相同。
我沒有使用UIView.animate來將按鈕向后移動到正確的位置,而是使用了另一個UIBezierPath來將按鈕沿同一圓圈向后移動。
這種解決方案避免了需要繼續設置按鈕的框架的問題,我認為這是我所有問題的根源-沖突的框架設置聲明。
使用自動布局時更改框架通常是一種不好的做法。 從外觀上看,您可以將對象設置為在完成動畫后不重置為原始位置,而不是將幀設置為動畫停止的位置:
anim.fillMode = kCAFillModeForwards
anim.removedOnCompletion = false
但請確保在添加動畫之前放置這些行
buttons[index].layer.addAnimation(anim, forKey: "animate position along path"+String(index))
為了避免在要向后移動按鈕時再次設置框架,應該進行基本相同的動畫處理,而不是
clockwise: false
將其設置為true
clockwise: true
然后從那里您可以將端角用作機芯返回的新起始角度,並將機芯的終止角度設置回您需要將按鈕放置到的任何位置
如果您只想彈跳它們,可以使用UIView.animateWithDuration(_,delay:_, usingSpringWithDamping:_,initialSpringVelocity:_,options:_,animations_,completed:_)
。
但是代碼中的問題是只有根動畫選項才是正確的。 僅第一個包含[.BeginFromCurrentState]
,也為所有動畫提供該選項。
此外,您也可以在這里將延遲用作選項。 不要將第二組動畫放在第一個動畫的已完成塊中,請delay
后再開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.