十一论剑之iOS项目实战(八)

自动拉伸问题

  • 从xib中加载进来的控件的autoresizingMask属性值默认是
    • UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
  • 如果一个控件显示出来的大小和当初设置的frame大小不一致,有可能是因为autoresizingMask属性值包含了UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight,解决方案
控件.autoresizingMask = UIViewAutoresizingNone;

属性名注意点

  • 对象属性名不能以new开头
@property (nonatomic, strong) NSMutableArray *newComments;

常见错误

-[__NSArray0 objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fb738c01870
// 错误地将NSArray当做NSDictionary来使用了

block细节

  • 如果【block内部】使用【外部声明的强引用】访问【对象A】, 那么【block内部】会自动产生一个【强引用】指向【对象A】
  • 如果【block内部】使用【外部声明的弱引用】访问【对象A】, 那么【block内部】会自动产生一个【弱引用】指向【对象A】

矩形框比较的2个函数

  • bool CGRectContainsRect(CGRect rect1, CGRect rect2)
    • 判断rect1是否包含了rect2
  • bool CGRectIntersectsRect(CGRect rect1, CGRect rect2)
    • 判断rect1和rect2是否有重叠
    • 注意:rect1和rect2要在同一个坐标系,比较结果才准确

转换坐标系总结

view2坐标系 : 以view2的左上角为坐标原点
view1坐标系 : 以view1的左上角为坐标原点

CGRect newRect = [view1 convertRect:rect fromView:view2];
// 让rect这个矩形框, 从view2坐标系转换到view1坐标系, 得出一个新的矩形框newRect
// rect和view2的含义 : 用来确定矩形框原来在哪

CGRect newRect = [view1 convertRect:rect toView:view2];
// 让rect这个矩形框, 从view1坐标系转换到view2坐标系, 得出一个新的矩形框newRect
// rect和view1的含义 :用来确定矩形框原来在哪

获得一个控件在window中的位置和尺寸

  • 以获得redView在window中的位置和尺寸为例
CGRect newRect = [[UIApplication sharedApplication].keyWindow convertRect:redView.bounds fromView:redView];
CGRect newRect = [[UIApplication sharedApplication].keyWindow convertRect:redView.frame fromView:redView.superview];
CGRect newRect = [redView convertRect:redView.bounds toView:[UIApplication sharedApplication].keyWindow];
CGRect newRect = [redView.superview convertRect:redView.frame toView:[UIApplication sharedApplication].keyWindow];
CGRect newRect = [redView convertRect:redView.bounds toView:nil];
CGRect newRect = [redView.superview convertRect:redView.frame toView:nil];

你可能感兴趣的:(十一论剑之iOS项目实战(八))