2017-09-04 150 views
2

我試圖在按鈕被點擊時從底部帶入子視圖。但只是第一次按鈕是可點擊的。在動畫按鈕不可點擊後再次點擊。第一次點擊後,UIButton不可點擊

這是代碼。

class AnimateView: UIView { 
var button: UIButton! 
var menuView: UIView! 
var mainView: UIView! 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     mainView = UIView(frame: CGRect(x: 0, y: 0, width: 
     self.frame.size.width, height: self.frame.size.height)) 

     mainView.clipsToBounds = true 
     mainView.backgroundColor = .clear 
     mainView.isUserInteractionEnabled = true 
     mainView.isExclusiveTouch = true 
     self.addSubview(mainView) 

     let theRect = CGRect(x: self.frame.size.width/2 - 44/2, y: 0, 
     width: 44, height: 44) 
     button = UIButton(frame: theRect) 
     check.layer.cornerRadius = btnView.frame.size.height/2 
     mainView.addSubview(self.check) 
     mainView.bringSubview(toFront: check) 
     check.addTarget(self, action: #selector(buttonClick(_:)), 
     for:.touchUpInside) 

     let newRect = CGRect(x: 0, y: check.frame.height, width: 
     self.frame.size.width, height: self.mainView.frame.size.height) 
     menuView = UIView(frame: newRect) 
     menuView.backgroundColor = .blue 
     mainView.addSubview(menuView) 

} 

required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func buttonClick(_ sender: UIButton) { 
     toggleMenu() 
    } 


    func toggleMenu() { 
     if check.transform == CGAffineTransform.identity { 
      UIView.animate(withDuration: 1, animations: { 
       self.check.transform = CGAffineTransform(scaleX: 12, y: 12) 
       self.mainView.transform = CGAffineTransform(translationX: 0, y: -120) 
       self.check.transform = CGAffineTransform(rotationAngle: self.radians(180)) // 180 is 3.14 radian 
       self.setNeedsLayout() 
      }) { (true) in 
       UIView.animate(withDuration: 0.5, animations: { 

       }) 
      } 
     } else { 
      UIView.animate(withDuration: 1, animations: { 
       // .identity sets to original position 
       //self.btnView.transform = .identity 
       self.mainView.transform = .identity 
       self.button.transform = .identity 
      }) 
     } 

    } 
} 

這個類是從另一個UIView類這樣調用

class MainViewClass: UIView { 
    var animateView: AnimateView! 

    override init(frame: CGRect) { 
      animateView = AnimateView(frame: CGRect(x: 0, y: self.frame.size.height - 44 - 44 - 20, width: self.frame.size.width, height: 122+44)) 
      //animateView.clipsToBounds = true 
      self.addSubview(animateView) 
      self.bringSubview(toFront: animateView) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

當屏幕上首次出現:

enter image description here

開始按鈕是可以點擊的,當它被點擊它隨着子視圖一起移動。 enter image description here

現在該按鈕不可點擊。當我看到按鈕的框架時,它與屏幕最初出現時的相同,即使按鈕觸摸屏幕底部,即使它向上移動。 我想要的是當點擊按鈕時從底部顯示帶有動畫的屏幕並且第二次點擊移動到默認位置。

但是,如果我把按鈕作爲內部自我子視圖,而不是作爲子視圖的MAINVIEW,則該按鈕爲可點擊的,但鑑於是動畫和向上移動即使它仍然在相同的位置。

self.addSubview(按鈕)

enter image description here

+0

你需要它完成動畫後刪除您'AnimateView',使按鍵再次暴露? –

+0

首先當點擊按鈕時,子視圖應該從屏幕底部開始動畫,並且按鈕也應該向上移動。然後第二次點擊按鈕應該與子視圖一起下去。 現在,當點擊按鈕時,子視圖與按鈕按鈕一起向上動畫不可點擊。 – bthapa

+0

單擊按鈕並向上移動時,該按鈕不可點擊。雖然我嘗試更新其框架,但它不可點擊。 – bthapa

回答

1

所以,我終於得到了我的意圖之前和之後的動畫實現的解決方案。

的原因的UIButton沒有接收到的動畫之後的任何觸摸事件是動畫之後,UIButton的向上移動,因爲它是MAINVIEW的子視圖。由於這一點,它超出了它的superView的界限,並且不響應任何點擊事件。儘管我將它的按鈕與其作爲mainView的superView一起移動,但按鈕沒有響應觸摸事件。 然後,我測試的使用結合的按鈕是否是MAINVIEW內則hitTest:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
     let pointForTargetView: CGPoint = button.convert(point, from: mainView) 
     if button.bounds.contains(pointForTargetView) { 
      print("Button pressed") 
      return button.hitTest(pointForTargetView, with:event) 
     } 
     return super.hitTest(point, with: event) 
    } 

此如果條件:

button.bounds.contains(pointForTargetView)

返回FALSE時動畫後按下按鈕。

所以,我需要捕獲的子視圖觸摸事件,超出了它的父框架。

使用則hitTest()解決了我的問題。 這是幫助我的鏈接。 Capturing touches on a subview outside the frame of its superview using hitTest:withEvent:

夫特3

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 

    if clipsToBounds || isHidden || alpha == 0 { 
     return nil 
    } 

    for subview in subviews.reversed() { 
     let subPoint = subview.convert(point, from: self) 
     if let result = subview.hitTest(subPoint, with: event) { 
      return result 
     } 
    } 

    return nil 
} 

的屏幕截圖: 當屏幕首次顯示,按鈕是在屏幕的底部,子視圖隱藏下面的按鈕。

enter image description here

當點擊該按鈕時,MAINVIEW向上動畫和按鈕向上移動,因爲它是MAINVIEW的子視圖。

enter image description here

當再次按下按鈕,MAINVIEW轉到原來的位置也是如此的按鈕。

enter image description here

相關問題