简体   繁体   English

一定大小时如何更改UIButton的大小

[英]How to change size of UIButton if it is a certain size

I have looked at and tried many things but cannot find a way that will. 我已经看过并尝试了许多方法,但是找不到找到方法。 What I am trying to do is create an if statement in my code that states that if one of the sender(a UIButton)'s dimensions, either height or width, are below 50, they would become a 50 x 50 UIButton equal to a randomColor() and randomPoint(). 我想做的是在我的代码中创建一个if语句,该语句声明如果发件人(UIButton)的尺寸之一(高度或宽度)小于50,则它们将变成50 x 50的UIButton,等于randomColor()和randomPoint()。 Thank you. 谢谢。 Below is my code. 下面是我的代码。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var scoreLabel: UILabel!

    var points: Int = 0

    func randomPoint() -> CGPoint {
        let randomPoint: CGPoint = CGPoint(x:CGFloat(arc4random()%320),y:CGFloat(arc4random()%568))
        return randomPoint
    }

    func randomColor() -> UIColor {
        let red = CGFloat(drand48())
        let green = CGFloat(drand48())
        let blue = CGFloat(drand48())
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }

    func spawnEnemy() {
        let enemy: UIButton = UIButton(frame: CGRect(x: 160, y: 160, width: 150, height: 150))
        enemy.backgroundColor = randomColor()
        enemy.center = randomPoint()
        enemy.addTarget(self, action: Selector("buttonPushed:"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(enemy)
    }

    func buttonPushed(sender : UIButton) {
        sender.backgroundColor = UIColor.blackColor()
        points = points + 1
        scoreLabel.textAlignment = .Center
        scoreLabel.text = "\(points)"
        scoreLabel.backgroundColor = UIColor.blackColor()
    }

    override func viewDidLoad() {
        super.viewDidLoad()



        NSTimer.scheduledTimerWithTimeInterval(0.75, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}

I have updated my code but I still do not think it is working. 我已经更新了我的代码,但是我仍然认为它不起作用。 I think it is because a lot of the time the squares are left as "L"s after a full overlapping square is clicked and you are left with an "L". 我认为这是因为在很多情况下,单击完全重叠的正方形后,将正方形保留为“ L”,然后留下“ L”。 Any way to fix this and make it so if will always work. 任何解决此问题的方法都可以解决。 My code is below in case it is actually or also something else? 我的代码在下面,以防万一,实际上还是其他?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var scoreLabel: UILabel!

    var points: Int = 0

    func randomPoint() -> CGPoint {
        let randomPoint: CGPoint = CGPoint(x:CGFloat(arc4random()%320),y:CGFloat(arc4random()%568))
        return randomPoint
    }

    func randomColor() -> UIColor {
        let red = CGFloat(drand48())
        let green = CGFloat(drand48())
        let blue = CGFloat(drand48())
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }

    func spawnEnemy() {
        let enemy: UIButton = UIButton(frame: CGRect(x: 160, y: 160, width: 150, height: 150))
        enemy.backgroundColor = randomColor()
        enemy.center = randomPoint()
        enemy.addTarget(self, action: Selector("buttonPushed:"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(enemy)
    }

    func buttonPushed(sender : UIButton) {
        sender.backgroundColor = UIColor.blackColor()
        points = points + 1
        scoreLabel.textAlignment = .Center
        scoreLabel.text = "\(points)"
        scoreLabel.backgroundColor = UIColor.blackColor()
        if sender.frame.height < 50 || sender.frame.width < 50 {
            sender.frame = CGRectMake(sender.frame.origin.x, sender.frame.origin.y, 50, 50)
            sender.backgroundColor = self.randomColor()
            sender.center = self.randomPoint()
            return
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()



        NSTimer.scheduledTimerWithTimeInterval(0.75, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}

If you're looking to make this check after the user pushes the button, you can add a check inside your buttonPressed: function like this: 如果您希望在用户按下按钮后进行此检查,则可以在buttonPressed:函数内部添加一个检查,如下所示:

func buttonPushed(sender : UIButton) {
    if sender.frame.height < 50 || sender.frame.width < 50 {
        sender.frame = CGRectMake(sender.frame.origin.x, sender.frame.origin.y, 50, 50)
        sender.backgroundColor = self.randomColor()
        sender.center = self.randomPoint()
        return
    }
    sender.backgroundColor = UIColor.blackColor()
    points = points + 1
    scoreLabel.textAlignment = .Center
    scoreLabel.text = "\(points)"
    scoreLabel.backgroundColor = UIColor.blackColor()
}

This will see if the height or width is less than 50 points, and if it is it'll set the size to 50x50, set it to a random color, and set the center to the random point. 这将查看高度或宽度是否小于50个点,如果将其设置为50x50,则将其设置为随机颜色,然后将中心设置为随机点。 It'll be an instant jump, though, so if you want to animate that you can do that instead. 但是,这将是一个瞬间的跳跃,因此,如果要设置动画,则可以执行此操作。 The return part will just ensure the rest of the code below doesn't get executed. return部分只会确保下面的其余代码不会被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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