2017-09-28 2 views
1

UIButton에서 두 개의 이미지 (애니메이션 포함)를 토글해야하지만 UIButton에서 내 이미지 뒤에 내 레이블을 숨 깁니다. 그래서 나는 그것에 UILabel의 하위 뷰를 추가했고 그것은 또한 보이지 않는다. 심지어 전방에 서브 뷰를 가져 오려고했지만 그것도 작동하지 않습니다.버튼 이미지를 애니메이트 할 때 UIButton 텍스트가 표시되지 않습니다.

let options = [option_1, option_2, option_6] 
var imageArray = [UIImage]() 

imageArray.append(UIImage(named: "light_button.png")!) 
imageArray.append(UIImage(named: "dark_button.png")!) 

let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.option_1.frame.width, height: self.option_1.frame.width)) 
label.text = "HEY" 
label.font = UIFont.systemFont(ofSize: 18) 
option_1.addSubview(label) 
option_1.bringSubview(toFront: label) 

for option in options{ 
    option?.setImage(UIImage(named: "green_button.png")!, for: .normal) 

    option?.imageView!.animationImages = imageArray 
    option?.imageView!.animationImages = imageArray 

    option?.setTitle("TEST TEXT", for: .normal) //Even this don't work 

    option?.imageView!.animationDuration = 2.35 
    option?.imageView!.startAnimating() 
} 

무엇이 잘못 되었습니까?

+0

옵션 배열에 무엇을 추가 했습니까? – Ishika

+0

@ishika 이들은 UIButtons입니다. – Prateekro

+0

아울렛을 만들거나 버튼을 프로그래밍 방식으로 만들지 않았습니까? – Ishika

답변

3

간단한 해결책을 제공하려고 노력할 것입니다. 버튼을 사용하여 스토리 보드 자체에서 선택한 상태와 기본 상태를 조정합니다. 예를 들어 버튼의 기본 상태를 선택하고 IBInspector에서 제목 및 이미지를 제공합니다. 그런 다음 선택된 상태에 대해 동일한 프로세스를 반복하고 상태 구성을 선택합니다. enter image description here

또한 이미지에 표시된대로 단추의 이미지와 제목을 관리 할 수도 있습니다. 당신은 또한 당신의 요구 사항에 따라 다양한 애니메이션 유형을 선택할 수 있습니다

 class ViewController: UIViewController { 
    @IBOutlet weak var buttonObj: UIButton! 
    var stop = false 
    var timer:Timer? 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
     // toggleButtons() 
     timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.toggleButtons), userInfo: nil, repeats: true); 
    } 

    func toggleButtons(){ 
     if stop{ 
      timer?.invalidate() 
     }else{ 
      buttonObj.toggleSelection() 
     } 


    } 

     @IBAction func btnClicked(_ sender: Any) { 

     //Once you click the button stop will become true and hence your timer will invalidate 
     stop = true 
    } 
} 
    //MARK:-******** UIButtonExtension *********** 
extension UIButton { 
    func toggleSelection() { 
     self.selected = self.selected ? false : true 
    } 
} 

: 아래 주어진 enter image description here

그런 다음에 마지막에 당신은 당신의 코드를 관리 할 수 ​​있습니다.

+0

다른 뷰에서 뷰로 이동합니다. 전구를 깜박이는 것과 같은 단추를 시작하기를 원합니다. (2 개의 이미지를 토글합니다.) 위의 해결 방법으로 가능합니까? – Prateekro

+0

클릭이있을 때까지 토글 루프를 계속하고 싶습니다. 옵션 중 하나를 선택하십시오. – Prateekro

+0

오 이런 매력처럼 작동합니다. 나는 나의 요구 사항을 충족시키기 위해 필요한 변경을했다. – Prateekro

0

이미지를 배경 이미지로 설정하려고 시도 했습니까? ?

옵션 .setBackgroundImage (있는 UIImage은 (이름 : .normal : 대한 "green_button.png")!) 상태 사이를 전환 할 때 정상 및 선택 상태를 위해있는 UIButton의 배경 이미지를 설정 한 후 애니메이션을 수

+0

예. @ kathiria하지만 애니메이션에서는 작동하지 않습니다. – Prateekro

0

(토글을 선택하고 버튼을 탭할 때 선택되지 않음)

+0

하지만 배경 이미지를 애니메이션으로 만들 수있는 방법을 찾지 못했습니다. 괜찮습니까? 그것을위한 뼈대를 제공하고 코드화합니다. 미리 감사드립니다. – Prateekro

관련 문제