Search code examples
swiftiad

iAd Interstitial touch activates scene button. Swift


I implemented iAd interstitial into my GameOverScene in SpriteKit. For some reason when the interstitial pops up, If i touch the same location as where a button is, I am still able to hear the button click. If the user touches this button by accident and closes the interstitial the scene will become black and I can not see the game anymore. Is there anyway how to solve this problem.

This is my code.

class GameOverScene: SKScene, ADInterstitialAdDelegate{

var interstitialAd:ADInterstitialAd!
var interstitialAdView: UIView = UIView()
var closeButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
var adBannerView = ADBannerView(frame: CGRect.zeroRect)

func loadInterstitialAd() {
    interstitialAd = ADInterstitialAd()
    interstitialAd.delegate = self
    scene?.paused = true
}

func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {

}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    closeButton.frame = CGRectMake(10, 10, 20, 20)
    closeButton.layer.cornerRadius = 10
    closeButton.setTitle("x", forState: .Normal)
    closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    closeButton.backgroundColor = UIColor.whiteColor()
    closeButton.layer.borderColor = UIColor.blackColor().CGColor
    closeButton.layer.borderWidth = 1
    closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)

    interstitialAdView.addSubview(closeButton)

    interstitialAdView = UIView()
    interstitialAdView.frame = self.view!.bounds
    view!.addSubview(interstitialAdView)

    interstitialAd.presentInView(interstitialAdView)
    UIViewController.prepareInterstitialAds()
    println("called after inter ad loads its content ")
}

func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {

    interstitialAdView.removeFromSuperview()
}

func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {

    return true
}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println("failed to receive")
    println(error.localizedDescription)

    closeButton.removeFromSuperview()
    interstitialAdView.removeFromSuperview()

}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    interstitialAdView.removeFromSuperview()
    println("user clicked out of ad")
}

override init(size: CGSize) {
    super.init(size: size)
    loadInterstitialAd()
}

Solution

  • try UIControlEvents.TouchUpInside vs. TouchDown A touch-up event in the control where the finger is inside the bounds of the control