[英]How to move button to random position? (Swift) while iAd is showing
我在這個問題中的答案一直很好:
但是由於我放了iAds,隨機位置停止工作,現在按鈕有時顯示在Main.storyboard中的真實位置,有時在屏幕內的隨機位置,這只是在iAd顯示時
這是代碼:
var newButtonX: CGFloat?
var newButtonY: CGFloat?
@IBAction func buttonPressed(sender: AnyObject) {
let buttonWidth = button.frame.width
let buttonHeight = button.frame.height
// Find the width and height of the enclosing view
let viewWidth = button.superview!.bounds.width
let viewHeight = button.superview!.bounds.height
// Compute width and height of the area to contain the button's center
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
// Generate a random x and y offset
let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))
// Offset the button's center by the random offsets.
newButtonX = xoffset + buttonWidth / 2
newButtonY = yoffset + buttonHeight / 2
circle.center.x = newButtonX!
circle.center.y = newButtonY!
}
override func viewDidLayoutSubviews() {
if let buttonX = newButtonX {
button.center.x = buttonX
}
if let buttonY = newButtonY {
button.center.y = buttonY
}
}
剛解決它,我在Main.storyboard上使用iAd bannerview,現在它工作正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.