我正在使用Swift中的SpriteKit
开发游戏。我希望在游戏场景中显示iAd
。
iAd
显示,但是当我点击它时,它没有反应。没有任何事情发生。
这是我的代码:
import UIKit
导入SpriteKit 导入iAd
类GameViewController:UIViewController,ADBannerViewDelegate,BackToMenuDelegate {
var bannerView:ADBannerView = ADBannerView()
var currentLevel:Int!
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view.
let skView = self.view as SKView
skView.multipleTouchEnabled = false
/* Set the scale mode to scale to fit the window */
let gameScene = GameScene(size:skView.bounds.size)
gameScene.scaleMode = .AspectFill
gameScene.currentLevel = currentLevel
gameScene.gameViewController = self
skView.presentScene(gameScene)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
bannerView.hidden = false
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
bannerView.hidden = true
}
func loadAds(){
bannerView.delegate = self
bannerView.hidden = true
var adRect = CGRectZero
let contentFrame = self.view.bounds
adRect.size = bannerView.sizeThatFits(contentFrame.size)
adRect.origin.y = 0
bannerView.frame = adRect
self.view.addSubview(bannerView)
}
func backToMenu(){
self.navigationController!.popToRootViewControllerAnimated(true)
let skView = self.view as SKView
skView.presentScene(nil)
}
func shareScore(score:Int){
var objectsToShare = [AnyObject]()
let textToShare = "I just got \(score) points in BlockCracker! Check out this App!"
objectsToShare.append(textToShare)
if let myWebsite = NSURL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=611627312&mt=8")
{
objectsToShare.append(myWebsite)
}
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypeMail,UIActivityTypeMessage]
self.presentViewController(activityVC, animated: true, completion: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
}
}
答案 0 :(得分:1)
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
其中willLeave
默认为false!只需更改即可返回真值。现在一切正常!
答案 1 :(得分:0)
我认为问题在于您的横幅广告视图为零(即CGRect.zeroRect
),因此没有可点击区域。你必须设置一个合适的框架;和sizeThatFits:
将帮助您获得适合当前窗口的横幅框架。 (Code adapted to Swift from this Obj-C answer.)
func loadAds(){
bannerView.delegate = self
bannerView.hidden = true
var adRect = CGRectZero
let contentFrame = self.view.bounds
adRect.size = bannerView.sizeThatFits(contentFrame.size)
// adRect.origin.y = CGRectGetHeight(contentFrame) - CGRectGetHeight(adRect) <--bottom of screen
adRect.origin.y = 0 // <-- top of screen
bannerView.frame = adRect
view.addSubview(bannerView)
}