美文网首页
UIView拾遗

UIView拾遗

作者: 事件_666 | 来源:发表于2017-04-28 11:02 被阅读0次

    1 clipsToBounds 当subView超出自己的边框时是否剪裁。

    Simulator Screen Shot 2017年4月28日 上午11.02.39.png
    2 bounds 面试时被虐了平时没注意
    每个人都会说bounds是对本坐标系的位置,但是不知道当改变with和height时是会以view的center为中心向外扩大。
    例如说
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
    [view1 setBounds:CGRectMake(-20, -20, 200, 200)];
    [view1 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:view1];
    view的frame为 {-40, -40}, {200, 200} 100分成两半一半50 减去原来的10 当然是-40了。
    3 -(instancetype)initWithCoder:(NSCoder *)aDecoder
    当view是从ib加载时调用这个方法,在这里自定义view。
    4 +(BOOL)requiresConstraintBasedLayout
    告诉系统哥们我是用的自动布局,请执行- (void)updateConstraints 方法吧。
    5 sizeThatFits:
    6 - (void)layoutSubviews setNeedsLayout
    ](apple-reference-documentation://hcKxHs2z9R)
    7 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    是否接收手势 用途做不规则手势的判断(例如判断点击是否在一个圆内) MIO5U.png

    9 tintColor(iOS7后) 会把父View的tintColor 传递过来 ,巧用此方法换肤
    当父viewtintcolor 改变时会触发 tintColorDidChange方法,apple公司也是用心良苦

    • (void)tintColorDidChange
      {
      NSLog(@"===tintColorDidChange===");
      _tintColorLabel.textColor = self.tintColor;
      _tintColorBlock.backgroundColor = self.tintColor;
      }
      10 mastView 会把mastView 的alph赋值给view 有人做过动画。
      11 layerClass可以重写这个方法自定义layer。
      12 exclusiveTouch 独占触摸事件 也就是说只响应当前试图。
      13 window 当前view的window 确实容易疏忽
      14 - (BOOL)isDescendantOfView:(UIView *)view 判断是不是子view
      NSLog(@"==:%d",[m_b1view isDescendantOfView:m_aview]);
      15 [sizeThatFits:](不改变frame). [sizeToFit](改变frame). 这两个API可以看成鸡肋没实际用途。

    16 布局相关--本来不屑一顾的方法竟然被虐了。
    A)- (void)layoutSubviews 注意旋转屏幕不会触发此方法 亲测(以前记得调用此方法)
    触发时机1)addSubView
    2)setFrame
    3) [m_cView setNeedsLayout]; [m_cView layoutIfNeeded];这两个哥们配合使用。
    B) translatesAutoresizingMaskIntoConstraints 默认yes
    17当一个View上有很多输入框时 调用此方法回收键盘 。

    • (BOOL)endEditing:(BOOL)force
      18 ib启动调用的方法
    • (instancetype)initWithCoder:(NSCoder *)aDecoder;
      19 截屏
    • (nullable UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
      生成的view是个中间类不能修改属性是只读的。是_UIReplicantView类型
      UIView *snapView = [m_aview snapshotViewAfterScreenUpdates:NO];
      [self.view addSubview:snapView];
      snapView.center = self.view.center;

    20 远程事件

    • (void)remoteControlReceivedWithEvent:(UIEvent *)event
      监控:上一曲下一曲
      21
      1 坐标转换虽然没啥高明但是很实用起码不用手工算了 两个配对使用
    • (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
    • (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
      2 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
    • (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view

    22 - (void)doesNotRecognizeSelector:(SEL)aSelector
    放弃一个方法
    app强调抛出一个异常

    If you override this method, you must call super
    or raise an [NSInvalidArgumentException
    ] exception at the end of your implementation. In other words, this method must not return normally; it must always result in an exception being thrown.

    23
    1 - (IMP)methodForSelector:(SEL)aSelector

    AClass *aClass = [[AClass alloc] init];
    IMP imp = [aClass methodForSelector:@selector(test:age:)];
    imp();//这里不要参数你懂的
    2 + (IMP)instanceMethodForSelector:(SEL)aSelector
    IMP imp2 = [AClass
    instanceMethodForSelector:@selector(test:age:)];
    imp2()
    24
    1 禁止copy

    • (BOOL)canPerformAction:(SEL)action withSender:(id)sender
      {
      if (action == @selector(copy:))
      {
      return NO;
      }
      return [super canPerformAction:action withSender:sender];
      }
      2 同上
    • (id)targetForAction:(SEL)action withSender:(id)sender

    251 一次被面试官问到这个方法是干什么的(日了狗了当时懵逼了),很简单历史遗留。_NSZone不在使用。

    • (instancetype)allocWithZone:(struct _NSZone *)zone

    相关文章

      网友评论

          本文标题:UIView拾遗

          本文链接:https://www.haomeiwen.com/subject/wdtwzttx.html