官术网_书友最值得收藏!

Adding new initializer

Extensions allow you to add new initializer to the currently available initializer for any particular type. For example, let's take a look at the CGRect class. CGRect has three initializers: empty init; init with origin and size; and init with x, y, width, and height. We will add new initializer with a center point and a rectangular size. Let's take a look at how to do it:

extension CGRect{ 
   init(center:CGPoint, size:CGSize){ 
       let x = center.x - size.width / 2 
       let y = center.y - size.height / 2 
       self.init(x: x, y: y, width: size.width, height: size.height) 
   } 
} 
 
let rect = CGRect(center: CGPoint(x: 50, y: 50), size: CGSizeMake(100, 80)) // {x 0 y 10 w 100 h 80} 
主站蜘蛛池模板: 海安县| 新巴尔虎右旗| 鲁甸县| 黄平县| 大同市| 东宁县| 呼玛县| 铜山县| 东安县| 洛浦县| 凌海市| 孝感市| 铁岭县| 肥东县| 壤塘县| 应用必备| 德钦县| 芦山县| 永顺县| 蒙城县| 阜新市| 板桥市| 灌阳县| 大埔县| 横峰县| 门头沟区| 永济市| 鄂尔多斯市| 扎鲁特旗| 工布江达县| 吴川市| 永州市| 凤阳县| 丰都县| 长汀县| 丰宁| 福清市| 内江市| 青海省| 锦州市| 巴彦淖尔市|