最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【基本解决】Swift中的offsetInPlace的含义和用法

Swift crifan 3697浏览 0评论

【背景】

折腾:

【已解决】swift中如何让一个图片和文字在一个矩形区域内水平居中对齐

期间,看到别处的代码中:

        let image = UIImageView(image: UIImage(named: image))
        image.frame.offsetInPlace(dx: (width - image.frame.size.width) / 2, dy: 0)
        self.addSubview(image)

用到了:

frame的offsetInPlace

想要搞懂具体语法含义和用法。

 

[折腾过程]

1.搜:

swift frame offsetInPlace

iOS offsetInPlace

参考:

CGGeometry – NSHipster

CGRectOffset

offsetBy / CGRectOffset: Returns a rectangle with an origin that is offset from that of the source rectangle.

SwiftObjective-C

// methods:

extension CGRect {

    func offsetBy(dx: CGFloat, dy: CGFloat) -> CGRect

    mutating func offsetInPlace(dx: CGFloat, dy: CGFloat)

}

// function:

func CGRectOffset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect

2.

CoreGraphics Changes for Swift

看起来是:

iOS 9.0中新加入的:

Added CGRect.offsetInPlace(dx: CGFloat, dy: CGFloat)

    @warn_unused_result(mutable_variant="offsetInPlace")

    func offsetBy(dx dx: CGFloat, dy dy: CGFloat) -> CGRect

    mutating func offsetInPlace(dx dx: CGFloat, dy dy: CGFloat)

但是还是不完全懂起具体用户和含义。

 

【总结】

frame.offsetInPlace好像是:

基于原有的frame,比如CGRect,去延伸x和y的值,形成一个新的矩形区域。

方便用于,基于原有的rect,去新创建一个类似大小的形状。

转载请注明:在路上 » 【基本解决】Swift中的offsetInPlace的含义和用法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.174 seconds, using 22.07MB memory