2017-12-17 42 views
2

나는 6 개의 버튼으로 구성된 그룹을 가지고 있습니다. 하나를 누르면 (프로그래밍 방식으로) 나는 빛을 발하고 밖으로 사라지기를 원합니다. 그래서 lightUp (버튼 : UIButton)이라는 함수를 만들었습니다. 조명과 조명이 꺼져있는 각 버튼에 대해 두 개의 이미지가 있습니다. 나는 기본 조명에서 켜짐으로 갈 수 있지만, 지금까지 버튼을 먼저 켜고 다시 켜기 전에 조명을 꺼 버리는 것이 문제였습니다. 아래의 코드에서 버튼은 전혀 켜지지 않습니다.UIButton 전환

func lightUp(button: UIButton){ 

    button.setImage(UIImage(named: padArrayOn[button.tag]), for: .normal) 

     UIView.transition(with: button, duration: 0.4, options: .transitionCrossDissolve, animations: { 
      button.setImage(UIImage(named: self.padArrayOff[button.tag]), for: .normal) 
     }) { (bool) in 
      self.playSequence() 
     } 
} 

답변

1

두 전환을 설정합니다. 첫 번째 완료 처리기에서 두 번째 전환은 아마도?

func lightUp(button: UIButton){ 
     UIView.transition(with: button, duration: 0.2, options: .transitionCrossDissolve, animations: { 
      button.setImage(UIImage(named: padArrayOn[button.tag]), for: .normal) 
      }, completion: {_ in 
       UIView.transition(with: button, duration: 0.2, options: .transitionCrossDissolve, animations: { 
        button.setImage(UIImage(named: self.padArrayOff[button.tag]), for: .normal) 
        }, completion: { _ in 
         self.playSequence() 
        } 
     )}) 
} 
0

정확하게 이해했다면 버튼을 켜고 싶을 수도 있습니다 (배경을 변경했을 수도 있음). 그리고 나서 페이드 아웃해야합니다.

이 같은 시도 할 수 있습니다 :

func lightUpAndFadeOut(button: UIButton){ 
    UIView.animate(withDuration: 2) { 
     button.backgroundColor = UIColor.red 
    } 
    UIView.animate(withDuration: 5) { 
     button.alpha = 0 
    } 
} 

모든 버튼 호출이 기능을하며 다음과 같아야합니다 올바른

lightUpAndFadeOut(button: sender as! UIButton) 

를?

+0

답변을 통해 내가 찾고있는 것과 비슷한 효과가 나타납니다. 감사합니다. 하지만 내 단추에 이미지를 사용하기 때문에 완전히 동일하지는 않습니다. – squarehippo10