2017-09-04 156 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

相关问题