繁体   English   中英

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

[英]How to move button to random position? (Swift) while iAd is showing

我在这个问题中的答案一直很好:

奇怪的@IBAction冲突或错误? (迅速)

但是由于我放了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM