애니메이션이 기본 버튼 애니메이션과 충돌하기 때문에 이러한 현상이 발생합니다.
다음
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을 배치하고 단추를 클릭하면 레이블에서 애니메이션을 수행합니다. 이 방법이 다소 "해킹"되는 것처럼 보일지라도 레이블이 애니메이션 처리되는 동안 버튼이 비활성화되어 있지 않아 빠른 순차 버튼 누르기를 등록 할 수 있다는 이점이 있습니다 (버튼의 목적이 사용자를 로그인하는 것처럼 보이지만, 어떤 경우에는 이것은 아마도 당신에게 필요하지 않을 것입니다).
고마워요! 내 노트북에있을 때 이것을 시험해 보겠습니다. –
완벽하게 작동합니다! –
환영 메이트 :) –