.xib调用内部ViewController无法识别触摸事件

时间:2016-07-30 08:07:47

标签: ios swift nib

UIViewController内,我调用.xib文件并将其呈现在当前的UIView上。

// initiate the pop up ad view and display it
let popUpAdView = PopUpAdViewController(nibName: "PopUpAdView", bundle: nil)
popUpAdView.displayIntoSuperView(view)

.xib文件中有一个按钮,当触摸它时,它应该从屏幕上移除。但它没有这样做。

我到底错过了什么?

class PopUpAdViewController: UIViewController {

@IBOutlet weak var popUpAdView: UIView!
@IBOutlet weak var closeButton: UIButton!

override func viewDidLoad() {
  super.viewDidLoad()

  // adjust the view size from the device's screen size
  let screenSize = UIScreen.mainScreen().bounds
  view.frame = CGRectMake(0, 64, screenSize.width, screenSize.height-64)

  // cosmetically adjust the view itself
  view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
  view.userInteractionEnabled = true
  closeButton.userInteractionEnabled = true

  // style the pop up ad view
  popUpAdView.layer.cornerRadius = 5
  popUpAdView.layer.shadowOpacity = 0.8
  popUpAdView.layer.shadowOffset = CGSizeMake(0.0, 0.0)

  closeButton.addTarget(self, action: #selector(removeOutOfSuperView), forControlEvents: .TouchUpInside)
}

func displayIntoSuperView(superView: UIView!) {
  superView.addSubview(self.view)

  // define the initial cosmetical values of the items
  popUpAdView.transform = CGAffineTransformMakeScale(0.3, 0.3)
  popUpAdView.alpha = 0.0
  closeButton.alpha = 0.0

  // animate...
  UIView.animateWithDuration(0.8, delay: 0.0, options: .CurveEaseIn, animations: {
    self.popUpAdView.alpha = 1.0
    self.popUpAdView.transform = CGAffineTransformMakeScale(1.0, 1.0)
  }) { (Bool) in
    UIView.animateWithDuration(0.6, delay: 1.5, options: .CurveEaseIn, animations: {
      self.closeButton.alpha = 1.0
      }, completion: { (Bool) in

    })
  }
}

func removeOutOfSuperView() {
  UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseIn, animations: {
    self.closeButton.alpha = 0.0
    self.popUpAdView.transform = CGAffineTransformMakeScale(0.1, 0.1)
  }) { (finished) in
    UIView.animateWithDuration(0.8, delay: 0.0, options: .CurveEaseIn, animations: {
      self.view.alpha = 0.0
      self.view.removeFromSuperview()
    }, completion: nil)
  }
}

@IBAction func closePopUpAdView(sender: AnyObject) {
  print("Closing the pop up ad...")
  removeOutOfSuperView()
}
}

更新

.xib structureL:

enter image description here

0 个答案:

没有答案