CGRect 函数

当访问一个 CGRectxywidthheight 时,应该使用[CGGeometry 函数][CGRect-Functions_1]代替直接访问结构体成员。苹果的 CGGeometry 参考中说到:

All functions described in this reference that take CGRect data structures as inputs implicitly standardize those rectangles before calculating their results. For this reason, 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.

推荐:

  1. CGRect frame = self.view.frame;
  2. CGFloat x = CGRectGetMinX(frame);
  3. CGFloat y = CGRectGetMinY(frame);
  4. CGFloat width = CGRectGetWidth(frame);
  5. CGFloat height = CGRectGetHeight(frame);

反对:

  1. CGRect frame = self.view.frame;
  2. CGFloat x = frame.origin.x;
  3. CGFloat y = frame.origin.y;
  4. CGFloat width = frame.size.width;
  5. CGFloat height = frame.size.height;

[CGRect-Functions_1]:http://developer.apple.com/library/ios/# documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html