Ios 使用arc4random更改按钮的帧?
我有一个按钮。我希望它在屏幕上的任意位置生成。它不会返回任何错误并运行,但屏幕上不会显示任何内容。我试着对这些值进行编码,结果成功了。我用的是Swift 代码:Ios 使用arc4random更改按钮的帧?,ios,swift,arc4random,Ios,Swift,Arc4random,我有一个按钮。我希望它在屏幕上的任意位置生成。它不会返回任何错误并运行,但屏幕上不会显示任何内容。我试着对这些值进行编码,结果成功了。我用的是Swift 代码: import UIKit class ViewController: UIViewController { let changingPoint = UIButton() let dot = UIImage(named: "sponsor-dot.png") override func viewDidLoad
import UIKit
class ViewController: UIViewController {
let changingPoint = UIButton()
let dot = UIImage(named: "sponsor-dot.png")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
changingPoint.setImage(dot, forState: .Normal)
changingPoint.addTarget(self, action: "pointPressed:", forControlEvents: .TouchUpInside)
changingPoint.frame = CGRectMake(CGFloat(arc4random()%(320)), CGFloat(arc4random()%(568)), 150, 150)
self.view.addSubview(changingPoint)
}
func pointPressed(sender: UIButton!) {
changingPoint.frame = CGRectMake(CGFloat(arc4random()%(320)), CGFloat(arc4random()%(568)), 150, 150)
println()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我相信你没有为按钮设置背景色
changingPoint.backgroundColor = UIColor.blackColor() //or any other color that you want
我认为你的图像加载不正确。尝试打印点以查看它是否为空:
println("image: \(dot)")
你的形象有多大?给定要传递给arc4random的值,按钮可能部分离开屏幕(或在屏幕边缘),如果图像明显小于150 x 150,您将看不到它。