2016-10-01 5 views
0

크로스 디졸브 애니메이션을 사용하여 버튼의 배경색을 변경하려고하는데, 이것을 실행할 때마다 즉시 부드러운 전환 대신 색상이 변경됩니다. 대신 내가보기 transition 전에 설정 한 다음보기의 transition를 시작하고 완벽하게 작동 한 backgroundColor 블록 animations에 설정Cross-Disolve 트랜지션이 순식간에 변경됩니다. Swift 3.0

UIView.transition(with: self.SignIn, 
        duration:3, 
        options: UIViewAnimationOptions.transitionCrossDissolve, 
        animations: { self.SignIn.backgroundColor? = UIColor(hexaString: "#" + hex) }, 
        completion: nil) 

답변

3

.

self.btnAnimate.backgroundColor = UIColor.red 
UIView.transition(with: self.btnAnimate, 
        duration: 3, 
        options: .transitionCrossDissolve, 
        animations: nil, 
        completion: nil) 

출력

enter image description here

참고 : 나는 당신이 이제까지 당신이 원하는 색상을 어떻게 설정할 수 있습니다 backgroundColorred 색상을 설정 한.

+0

고마워요! 내 노트북에있을 때 이것을 시험해 보겠습니다. –

+0

완벽하게 작동합니다! –

+0

환영 메이트 :) –

0

애니메이션이 기본 버튼 애니메이션과 충돌하기 때문에 이러한 현상이 발생합니다.

다음
import UIKit 

import UIKit 

class CustomButton: UIButton { 

    func animateButton(hex: String) { 

     // This block disables the default animations 
     UIButton.performWithoutAnimation { 

      // This is where you define your custom animation 
      UIView.transition(with: self, duration:3, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { 
       self.backgroundColor? = UIColor(hexaString: "#" + hex) }, completion: nil) 

      // You must call layoutIfNeeded() at the end of the block to prevent layout problems 
      self.layoutIfNeeded() 
     } 
    } 

} 

호출이 기능 :

이 문제를 해결하려면있는 UIButton의 사용자 지정 하위 클래스를 만들 수 있습니다 물론

signInButton.animateButton("ffffff") // Pass in your hex string 

를, 당신의 loginButton의 클래스를 변경해야합니다 이야기 게시판에서 UIButton부터 CustomButton까지

이 방법의 단점은 애니메이션 프로세스 중에 텍스트가 여전히 어둡다는 것입니다. 이 문제를 재정의하는 방법이있을 수 있습니다 (어떻게 신속하게 결정할 수 없었는지).이 솔루션에 대한 또 다른 접근법은 UIButton 대신 UILabel을 사용하고 클릭 리스너를 설정하는 것입니다.

또는 다른 방법을 고려해 볼 수도 있습니다. UButton 뒤에 customLabel을 배치하고 단추를 클릭하면 레이블에서 애니메이션을 수행합니다. 이 방법이 다소 "해킹"되는 것처럼 보일지라도 레이블이 애니메이션 처리되는 동안 버튼이 비활성화되어 있지 않아 빠른 순차 버튼 누르기를 등록 할 수 있다는 이점이 있습니다 (버튼의 목적이 사용자를 로그인하는 것처럼 보이지만, 어떤 경우에는 이것은 아마도 당신에게 필요하지 않을 것입니다).