Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageapplescript
themeMidnight
typedef struct UIEdgeInsets {
    CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;

...

创建

Code Block
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
    UIEdgeInsets insets = {top, left, bottom, right};
    return insets;
}

...

Code Block
languageapplescript
themeMidnight
UIKIT_STATIC_INLINE CGRect UIEdgeInsetsInsetRect(CGRect rect, UIEdgeInsets insets) {
    rect.origin.x    += insets.left;//x轴右移left个单位(left<0时则左移)
    rect.origin.y    += insets.top;//y轴下移top个单位(top<0时则上移)
    rect.size.width  -= (insets.left + insets.right);//宽减少left+right
    rect.size.height -= (insets.top  + insets.bottom);//高减少top+bottom
    return rect;
}

这里需要注意的就是顺序问题。定义中是UIEdgeInsets insets = {top, left, bottom, right}; 

...

  1. left
  2. top
  3. left+right
  4. top+ bottom

例子

先看一个Catagory.目的是增加UIButton的相应范围.

Code Block
@implementation UIButton (TouchResponse)
- (void)touchResponseWithVector:(CGVector)vector {
    DLog(@"self.frame:%@",NSStringFromCGRect(self.frame));
    CGRect touchFrame = CGRectInset(self.frame, vector.dx, vector.dy);
    DLog(@"touchFrame:%@",NSStringFromCGRect(touchFrame));
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(-vector.dy, -vector.dx, -vector.dy, -vector.dx);
    self.imageEdgeInsets = edgeInsets;
    self.frame = touchFrame;
}
@end