广告加载时按钮消失

时间:2016-10-02 19:37:17

标签: ios swift admob

我用一些按钮编写了一个应用程序。其中一个通过按下它来显示插页式广告。当我关闭广告时,视图上的所有按钮都会消失。我该如何解决这个问题?

//Collection Sound
    var boomSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "135936__bradwesson__collectcoin", ofType: "wav")!)
    var audioPlayer = AVAudioPlayer()



@IBOutlet var ScoreLabel: UILabel!
@IBOutlet weak var BT6: UIButton!
@IBOutlet weak var BT5: UIButton!
@IBOutlet weak var BT4: UIButton!
@IBOutlet weak var BT3: UIButton!
@IBOutlet weak var BT2: UIButton!
@IBOutlet weak var BT1: UIButton!

var interstitial: GADInterstitial!

//When reaching  x Taps
var taps = Int(){
    didSet {
        if taps == 330 {
            print("You have reached 5 taps !!")
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()





    //load interstitial Ad
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-1469592343938512/2951120388")
    let request2 = GADRequest()
    interstitial.load(request2)



    authPlayer()

    let defaults = UserDefaults.standard
    if let storedTaps = defaults.object(forKey: "key") as? Int {
        self.taps = storedTaps
        setLabel(storedTaps)

    }

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


}

//HIDE STATUS BAR   
override var prefersStatusBarHidden : Bool {
    return true
}


@IBAction func BTN(_ sender: AnyObject) {

    sender.setTitleColor(UIColor.red, for: UIControlState())

    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

}

func setLabel(_ taps:Int) {
    ScoreLabel.text = "TAPS \(taps)"
}

//GameCenter
func authPlayer(){

    let localPlayer = GKLocalPlayer.localPlayer()

    localPlayer.authenticateHandler = {
        (view, error) in

        if view != nil {

            self.present(view!, animated: true, completion: nil)

        }
        else {

            print(GKLocalPlayer.localPlayer().isAuthenticated)


        }
    }
}

//GameCenter
func saveHighscore( _ number : Int){

    if GKLocalPlayer.localPlayer().isAuthenticated {

        let scoreReporter = GKScore(leaderboardIdentifier: "TAPME")

        scoreReporter.value = Int64(number)

        let scoreArray : [GKScore] = [scoreReporter]

        GKScore.report(scoreArray, withCompletionHandler: nil)

    }

}

//GameCenter
func showLeaderboard(){

let viewController = self.view.window?.rootViewController

    let gcvc = GKGameCenterViewController()

    gcvc.gameCenterDelegate = self

    viewController?.present(gcvc, animated: true, completion: nil)

}

//GameCenter
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
    gameCenterViewController.dismiss(animated: true, completion: nil)
}

//Reset TapsLabel
@IBAction func reset(_ sender: AnyObject) {

    taps = 0
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

    //load interstitial Ad
    if (interstitial.isReady){

        interstitial.present(fromRootViewController: self)
        interstitial = createAD()




    }


}

//Load GameCenter
@IBAction func CallGC(_ sender: UIButton) {

    showLeaderboard()
    saveHighscore(taps)
}

//When view is loaded
override func viewDidAppear(_ animated: Bool) {
    BT6.isHidden = true
    BT5.isHidden = true
    BT4.isHidden = true
    BT3.isHidden = true
    BT2.isHidden = true
    //Hiding all but one button when the view controller loads
}

//Random button view
@IBAction func BT6(_ sender: AnyObject) {
    //this checks when BT6 is pressed and then hides it
    BT6.isHidden = true
    let random = Int(arc4random_uniform(UInt32(4)))
    if random == 0 {
        BT5.isHidden = false
    } else if random == 1 {
        BT4.isHidden = false
    } else if random == 2 {
        BT3.isHidden = false
    } else if random == 3 {
        BT2.isHidden = false
    } else if random == 4 {
        BT1.isHidden = false
    }
    //this part creates a randomiser between 0-4 and depending on which number turns out, it will hide a certain button
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        // Catch exception
    }



    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

}
@IBAction func BT5(_ sender: AnyObject) {
    BT5.isHidden = true
    let random = Int(arc4random_uniform(UInt32(4)))
    if random == 0 {
        BT6.isHidden = false
    } else if random == 1 {
        BT4.isHidden = false
    } else if random == 2 {
        BT3.isHidden = false
    } else if random == 3 {
        BT2.isHidden = false
    } else if random == 4 {
        BT1.isHidden = false
    }


    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")


    do {
        audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        // Catch exception
    }



}
@IBAction func BT4(_ sender: AnyObject) {
    BT4.isHidden = true
    let random = Int(arc4random_uniform(UInt32(4)))
    if random == 0 {
        BT5.isHidden = false
    } else if random == 1 {
        BT6.isHidden = false
    } else if random == 2 {
        BT3.isHidden = false
    } else if random == 3 {
        BT2.isHidden = false
    } else if random == 4 {
        BT1.isHidden = false
    }


    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        // Catch exception
    }

}
@IBAction func BT3(_ sender: AnyObject) {
    BT3.isHidden = true
    let random = Int(arc4random_uniform(UInt32(4)))
    if random == 0 {
        BT5.isHidden = false
    } else if random == 1 {
        BT4.isHidden = false
    } else if random == 2 {
        BT6.isHidden = false
    } else if random == 3 {
        BT2.isHidden = false
    } else if random == 4 {
        BT1.isHidden = false
    }


    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        // Catch exception
    }

}
@IBAction func BT2(_ sender: AnyObject) {
    BT2.isHidden = true
    let random = Int(arc4random_uniform(UInt32(4)))
    if random == 0 {
        BT5.isHidden = false
    } else if random == 1 {
        BT4.isHidden = false
    } else if random == 2 {
        BT3.isHidden = false
    } else if random == 3 {
        BT6.isHidden = false
    } else if random == 4 {
        BT1.isHidden = false
    }


    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        // Catch exception
    }

}
@IBAction func BT1(_ sender: AnyObject) {
    BT1.isHidden = true
    let random = Int(arc4random_uniform(UInt32(4)))
    if random == 0 {
        BT5.isHidden = false
    } else if random == 1 {
        BT4.isHidden = false
    } else if random == 2 {
        BT3.isHidden = false
    } else if random == 3 {
        BT2.isHidden = false
    } else if random == 4 {
        BT6.isHidden = false
    }


    taps += 1
    setLabel(taps)
    let defaults = UserDefaults.standard
    defaults.set(taps, forKey: "key")

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        // Catch exception
    }

}

}

//interstitial Ad
func createAD() -> GADInterstitial{

    let interstitial = GADInterstitial(adUnitID: "ca-app-pub-1469592343938512/2951120388")
    interstitial.load(GADRequest())
    return interstitial
}

1 个答案:

答案 0 :(得分:0)

每次视图出现在屏幕上时都会调用此方法,而不是在加载时。在ViewDidLoad方法中移动该代码

     string strParam;          
        string Path_FFMPEG = @"C:\Windows\system32\cmd.exe";
        string WorkingDirectory = @"C:\Users\Braintech\documents\visual studio 2013\Projects\convertVideo\convertVideo";            
        string command1 = "ffmpeg -i video1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts";
        string command2 = "ffmpeg -i video2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts";
        string command3 = "ffmpeg -i" + "concat:intermediate1.ts|intermediate2.ts" + " -c copy -bsf:a aac_adtstoasc Output.mp4";
        string Command = @"" + command1 + " & " + command2 + " & " + command3 + " ";      

        strParam = "ffmpeg -f concat -i " + file + " -c copy " + strResult;

        process(Path_FFMPEG, Command, WorkingDirectory);
   public static void process(string Path_FFMPEG, string Command, string WorkingDirectory)
    {
        try
        {           

            Process ffmpeg = new Process();
            ProcessStartInfo ffmpeg_StartInfo = new ProcessStartInfo(Path_FFMPEG, Command);
            ffmpeg_StartInfo.WorkingDirectory = WorkingDirectory;              
            ffmpeg_StartInfo.UseShellExecute = false;
            ffmpeg_StartInfo.RedirectStandardError = true;
            ffmpeg_StartInfo.RedirectStandardOutput = true;
            ffmpeg.StartInfo = ffmpeg_StartInfo;
            ffmpeg_StartInfo.CreateNoWindow = true;
            ffmpeg.EnableRaisingEvents = true;
            ffmpeg.Start();
            ffmpeg.WaitForExit(30000);              
            ffmpeg.Close();
            ffmpeg.Dispose();
            ffmpeg = null;
        }
        catch (Exception ex)
        {

        }
    }