直接访问CGRect值与在Swift中对其进行规范化-Objective-C规则仍然有效吗? [英] Accessing CGRect values directly vs normalizing them in Swift - Objective-C rules still valid?

查看:105
本文介绍了直接访问CGRect值与在Swift中对其进行规范化-Objective-C规则仍然有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的灵感来自安德鲁·卡特(Andrew Carter)对上一个有关Swift中新的 CGSize 初始值设定项的问题的评论

This question is inspired by Andrew Carter's comment on a previous question about the new CGSize initializer in Swift.

用于CGGeometry的Apple文档说:


...您的应用程序
应该避免直接读取和写入
CGRect数据结构中存储的数据。取而代之的是,使用此处描述的函数来
操纵矩形并检索其特征。

... your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.

Apple建议不要直接访问 CGRect 中的数据在Swift中仍然有效?为什么应使用 CGRectGetMidX CGRectGetWidth 等代替访问的值当这些属性现在通过Swift在 CGRect 上的新扩展名公开时,直接使用CGRect 结构?

Is Apple's recommendation to not directly access the data in a CGRect still valid with Swift? Why should CGRectGetMidX, CGRectGetWidth, etc. be used in place of accessing the values of a CGRect struct directly, when these properties are now exposed with Swift's new extension on CGRect?

推荐答案

考虑宽度和高度为负的非标准 CGRect

Consider a non-standard CGRect with a negative width and height:

var rect = CGRect(x: 0.0, y: 0.0, width: -10.0, height: -10.0)

根据Apple文档,这是一个有效的矩形,例如原点为 [0.0,0.0] 且大小为 [10.0,10.0] 等效于原点为 [10.0,10.0] 且大小为 [-10.0,-10.0]

This is a valid rectangle according to the Apple docs, as "a rectangle with an origin of [0.0, 0.0] and a size of [10.0, 10.0] is exactly equivalent to a rectangle with an origin of [10.0, 10.0] and a size of [-10.0, -10.0]."

您可以对此<$ c $进行标准化c> CGRect ,方法是调用旧的内联 CGRectStandardize 方法,就像在Objective-C中一样,或者调用<$的Swift扩展中提供的任何新方法c $ c> CGRect

You can standardize this CGRect by calling the legacy inline CGRectStandardize method like in Objective-C, or any of the new methods provided on the Swift extension of CGRect:

CGRectStandardize(rect) // {x -10 y -10 w 10 h 10}
rect.standardized // {x -10 y -10 w 10 h 10}
rect.standardizeInPlace() // {x -10 y -10 w 10 h 10}

但是等等!这将使rect在坐标平面上重新定位,不仅使宽度和高度为正,而且使 origin 为负数,以反映矩形的初始位置及其负的宽度和高度。

But wait! This will reposition your rect on the coordinate plane, not only making your width and height positive, but making your origin negative to reflect the initial position of the rect with its negative width and height.

内联 CGRectGet 函数提供了一个接口,可以在不更改其来源的情况下标准化您的rect的特定值。 Swift在 CGRect 上提供了扩展,因此您可以直接访问规范化的值,而不必使用 CGGeometry :

The inline CGRectGet functions provide an interface to normalize a specific value of your rect, without changing its origin. Swift provides an extension on CGRect so you can access the normalized values directly, rather than using the legacy C methods provided by CGGeometry:

var rect = CGRect(x: 0.0, y: 0.0, width: -10.0, height: -10.0)

rect.size.width // non-normalized, returns -10
CGRectGetWidth(rect) // bridged C function, normalized, returns 10
rect.width // new from Swift extension on CGRect, normalized, returns 10

新接口:

extension CGRect {
    // ...
    public var width: CGFloat { get }
    public var height: CGFloat { get }
    public var minX: CGFloat { get }
    public var midX: CGFloat { get }
    public var maxX: CGFloat { get }
    public var minY: CGFloat { get }
    public var midY: CGFloat { get }
    public var maxY: CGFloat { get }
    // ...
}

所以答案是的,Objective-C中 CGRect 的相同规则也适用于Swift。唯一的区别是,Swift在某些 CGGeometry 结构上提供了扩展,使您可以摆脱从 CGGeometry 标头。

So the answer is yes, the same rules for CGRect in Objective-C apply in Swift as well. The only difference here is that Swift provides an extension on some CGGeometry structs which allow you to move away from the old inline C functions bridged from the CGGeometry headers.

这篇关于直接访问CGRect值与在Swift中对其进行规范化-Objective-C规则仍然有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆