2017-06-26 21 views
1

にはUIButtonがあります。それは白いUIColorと橙色の強調表示された色をしています。色をハイライトされた色から別の色に直接切り替える

ボタンを選択した直後にこのボタンの色を変更したいと思います。次のコードで

理想的な結果

White(default) -> orange(highlighted) -> green(animated) -> white(default) 

しかしながら、橙色から緑色に色が変化する前に、それはすぐに白色となります。

現在の結果

White(default) -> orange(highlighted) -> White(default) -> green(animated) -> white(default) 

は、どのように私は緑色にハイライト表示され、オレンジ色から直接色を切り替えることができますか?

UIView.animate(withDuration:0, animations: {() -> Void in 
     cell.buttons[index].backgroundColor = UIColor.green 

    }) { (Bool) -> Void in 
     UIView.animate(withDuration: 0.5, animations: {() -> Void in 
      cell.buttons[index].backgroundColor = UIColor.green 

     }, completion: { (Bool) -> Void in 
      UIView.animate(withDuration: 0, animations: {() -> Void in 
       cell.buttons[index].backgroundColor = UIColor.white 
      }, completion:nil) 
     }) 
    } 
+0

あなたのUIButtonのタイプは何ですか?ストーリーボードでカスタムに設定されていることを確認してください。 –

+0

あなたのボタンにオレンジの "ハイライトされた色"を得るためにどのような方法を使用していますか? – DonMag

答えて

1

アニメーションコードは大丈夫ですが、アニメーションは2つの状態間のトランザクションであり、時間(期間)が必要です。したがって、0秒のアニメーションを持たないようにしてください。その場合、アニメーションは役に立たないからです。

ボタンリスナーに間違いがあるようです。ボタンをクリックするとすぐに、色をオレンジ色に変更したいとします。つまり、touchDownです。そして、あなたはので、これを試してみるあなたのviewDidLoad

yourButton.addTarget(self, action:#selector(btnShowPasswordClickHoldDown), for: .touchDown) 

yourButton.addTarget(self, action:#selector(btnShowPasswordClickRelease), for: .touchUpInside) 

にこのコードを追加し、有効期間

でアニメーションを追加 touchUpInside

である、とすぐにボタンがリリースされる色の変化をやりたいです

func btnShowPasswordClickHoldDown(){ 
    UIView.animate(withDuration: 0.5, animations: {() -> Void in 
     self.yourButton.backgroundColor = UIColor.orange 
    }, completion:nil) 
} 

func btnShowPasswordClickRelease(){ 
    UIView.animate(withDuration: 0.5, animations: {() -> Void in 
     self.yourButton.backgroundColor = UIColor.green 

    }, completion: { (Bool) -> Void in 
     UIView.animate(withDuration: 0.5, animations: {() -> Void in 
      self.yourButton.backgroundColor = UIColor.white 
     }, completion:nil) 
    }) 
} 
関連する問題