用arc4random改变按钮的框架?

时间:2014-06-09 03:37:13

标签: ios swift arc4random

我有一个UIButton。我想让它在屏幕上的随机位置生成。它不会返回错误并运行,但屏幕上不会显示任何内容。我尝试对值进行硬编码,这很有效。我正在使用Swift。

CODE:

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


}

2 个答案:

答案 0 :(得分:1)

我相信你没有为按钮设置背景颜色。

changingPoint.backgroundColor = UIColor.blackColor() //or any other color that you want

答案 1 :(得分:0)

我认为您的图片无法正常加载。尝试打印点以查看它是否为空:

println("image: \(dot)")
相关问题