How do I move a button to a random position? (Swift) while iAd shows

I am doing great with the answer in this question:

Strange @IBAction conflict or error? (Swift)

But since I place the iAds, the random position stops working so well, now the button is sometimes displayed in its real place in the Main.storyboard and sometimes in a random place inside the screen, this only happens when the iAd is showing

here is the code:

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 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 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
    }
} 

      

+3


source to share


1 answer


Just decide I am using iAd bannerview on Main.storyboard and it works fine now



0


source







All Articles