如何将按钮移动到随机位置? (快速),而iAd正在展示

时间:2014-12-22 08:38:34

标签: ios swift

我在这个问题的答案中做得很好:

Strange @IBAction conflict or bug? (Swift)

但是因为我放了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
    }
} 

1 个答案:

答案 0 :(得分:0)

解决它,我在Main.storyboard上使用iAd bannerview,现在它可以正常工作