drawRect UIView にバナーを配置したかったのですが、「bannerView.rootViewController = self」という行で警告が表示されます。そのため、バナーをviewControllerに配置しました。drawRect の値でバナーの位置を変更します。値は印刷できますが、バナーは移動を拒否します。コードは次のとおりです。
ビューコントローラー:
class ViewController: UIViewController, GADBannerViewDelegate,ChangeBannerDelegate
// var bannerView:GADBannerView! // shows nil when run func changeBanner(), has to use the following
var bannerView = GADBannerView(adSize: kGADAdSizeBanner)
override func viewDidLoad() {
super.viewDidLoad()
waveView = WaveWater(frame: self.view.bounds)
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.frame = CGRect(x: 0, y: self.view.bounds.height-50, width: self.view.bounds.width, height: 50)
bannerView.adUnitID = "xxxxxxxxxxxx"
bannerView.rootViewController = self
bannerView.delegate = self
self.view.addSubview(bannerView)
bannerView.loadRequest(GADRequest())
// set other things...
}
func changeBanner(y:CGFloat){
bannerView.frame.origin.y = y
println(bannerView.frame.origin.y) // can print 'y', and banner.frame.origin.y was changed, but banner stays where it is
}
UIView:
protocol ChangeBannerDelegate:NSObjectProtocol,GADBannerViewDelegate{
func changeBanner(y:CGFloat)
}
class WaveWater: UIView, UIGestureRecognizerDelegate {
var bannerDelegate:ChangeBannerDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.bannerDelegate = ViewController()
changeBanner(50) // place it here just for a try
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
//drawing things........
}
}