2017-09-04 11 views
2

ボタンをクリックすると、下からサブビューを取得しようとしています。しかし、ボタンがクリック可能なのは初めてです。アニメーションの後の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 
      }) 
     } 

    } 
} 

このクラスは、この

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") 
    } 
} 

のような別のUIViewクラスから呼び出されます。

enter image description here

最初のボタンがクリック可能で、それはそれをクリックされたときサブビューに沿って上に移動します。 enter image description here

ボタンはクリックできません。ボタンの枠を見たとき、上に上がってもボタンが画面の底に触れて画面が最初に表示されたときと同じです。 私が意図しているのは、ボタンをクリックしたときに下からアニメーションのある画面を表示し、2回目のクリックでデフォルトの位置に移動することです。

ボタンをmainviewのサブビューではなく、selfの中にサブビューとして配置すると、ボタンはクリック可能ですが、ビューがアニメーション化されて上に移動した後も同じ位置に残ります。

self.addSubview(ボタン)

enter image description here

+0

あなたはそれがアニメーション化完了だ後、あなたの 'AnimateView'を削除する必要がありますので、ボタンが再び公開されますか? –

+0

ボタンを最初にクリックすると、サブビューが画面の下からアニメーション表示され、ボタンも上に移動する必要があります。次に、2回目のクリックで、ボタンがサブビューとともに表示されます。 今、ボタンをクリックすると、サブビューが上に移動し、ボタンのボタンはクリックできません。 – bthapa

+0

ボタンをクリックして上に移動すると、クリックできません。私はフレームを更新してみましたが、クリックできませんでした。 – bthapa

答えて

1

だから、私は最終的に私が前に、アニメーションの後に達成するために意図されたものの溶液を得ました。

アニメーションの後にUIButtonがタッチイベントを受け取らないのは、アニメーションの後にUIButtonがmainViewのサブビューとして上に移動するためです。このため、スーパービューの範囲外であり、クリックイベントに応答しません。メインビューのスーパービューと一緒にボタンを移動しましたが、ボタンがタッチイベントに反応しませんでした。 はその後、私はバインドボタンを使用して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) 
    } 

これをもし条件:

falseを返します

button.bounds.contains(pointForTargetView)アニメーション後にボタンが押されます。

私はスーパービューフレームから出ているサブビューでタッチイベントをキャプチャする必要があります。

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

関連する問題