ios 在 CGRectMake、Swift、UIkit 中使用变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30692096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:15:02  来源:igfitidea点击:

Using variables in CGRectMake, Swift, UIkit

iosxcodeswiftuikit

提问by Mats

For an App I'm making i need to use variables to change the size and position of objects (Labels). I've tried var example = CGRectMake(0, 0, 0, 100), hoping it would ignore the zeros (Not really thinking it would though). I then tried:

对于我正在制作的应用程序,我需要使用变量来更改对象(标签)的大小和位置。我试过var example = CGRectMake(0, 0, 0, 100),希望它会忽略零(不是真的想它虽然)。然后我尝试:

var example = 100
Label1.frame = CGRectMake(20, 20, 50, example)

I changed the syntax a bit, adding "" and replacing the CGRectMakewith CGRectetc, but nothing worked... I don't get what I'm doing wrong here... Help!

我改变了语法了一下,加上“”和更换CGRectMakeCGRect等,但是毫无效果......我不明白我在做什么错在这里...帮助!

采纳答案by Aaron Rasmussen

CGRectMaketakes CGFloats for all of its arguments. Your sample code should work fine if you specify that exampleis supposed to be a CGFloat, using a type identifier:

CGRectMakeCGFloats 作为其所有参数。如果您使用类型标识符指定example应该是 a CGFloat,您的示例代码应该可以正常工作:

         //  v~~~~ add this...
var example: CGFloat = 100
Label1.frame = CGRectMake(20, 20, 50, example)

Otherwise, swift infers the type of exampleto be Int, and the call to CGRectMakefails, cuz it can't take an Intas a parameter...

否则,swift 会推断出exampleto的类型Int,并且调用CGRectMake失败,因为它不能将 anInt作为参数......

回答by quemeful

Swift 3 update

斯威夫特 3 更新

let rect = CGRect(x: 0, y: 0, width: 100, height: 100)

回答by Chris

So, there is many ways to skin the cat. It all depends what your needs and requirements are (maybe you could elaborate a bit on what you are trying to achieve?). But one way to do it could be to set a variable when something happens, and then update the frame of the label. If you added a tap gesture recognizer to your view, and updated your label like so:

所以,有很多方法可以给猫剥皮。这完全取决于您的需求和要求是什么(也许您可以详细说明您要实现的目标?)。但一种方法是在发生某些事情时设置一个变量,然后更新标签的框架。如果你在你的视图中添加了一个点击手势识别器,并像这样更新你的标签:

let myLabel = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()

    let tapGestRecog = UITapGestureRecognizer(target: self, action: "handleTap:")
    self.view.addGestureRecognizer(tapGestRecog)
}

func handleTap(sender:UIGestureRecognizer) {

    let newXposition = sender.locationInView(self.view).x
    let newYposition = sender.locationInView(self.view).y

    myLabel.frame = CGRectMake(newXposition, newYposition, 200, 200)
}

This is just an example, and a very crude way of doing it. There are many other ways of doing it, but it hopefully gives you an idea of how to achieve it.

这只是一个例子,而且是一种非常粗略的做法。有很多其他方法可以做到这一点,但希望它能让您了解如何实现它。

回答by Duncan C

Swift allows syntax that Objective-C does not:

Swift 允许使用 Objective-C 不允许的语法:

var example = 100
label.frame.size.height = example

In objective-C you would have to do it differently:

在 Objective-C 中,您必须以不同的方式执行此操作:

CGRect frame = label.frame;  //Create a temporary rect to hold the frame value
frame.size.height = example;
label.frame = frame;