美文网首页程序员
iOS 常用控件的继承关系

iOS 常用控件的继承关系

作者: JustEverOnce | 来源:发表于2017-10-31 12:36 被阅读0次

    开发时间久了,一些基础的东西会比较模糊,特别是理论方面会有所欠缺,今天就整理下一些常用控件的继承关系,也算是重新温习下基础吧

    Objective-C 中所有的类都是继承自NSObject(别问为什么,我有点方),我们常用的控件本质上其实也都是类,所以最终的顶层父类也就毫无疑问是NSObject

    今天主要讲控件,iOS开发中的控件呢,说白了就是用户所能看到的所有东西基本上都可以称之为控件,UIButton,UILabel,UIScrollView......

    控件是可以交互的,那么接受这些交互的类呢,就是UIResponder,简单的示例,UIView是可以触摸的吧(Touch),比较常见的监听touch事件的方法

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;//触摸屏幕
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;//在屏幕上移动
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//离开屏幕
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//系统事件干扰
    

    这些方法呢,其实不是UIView的方法,他是UIResponder的方法
    所以单从控件的角度来说,UIResponder也是继承关系中处于相对顶端的位置

    UIView:所有控件的父类或者说是间接父类,UIView它本身呢是继承自UIResponder,UIResponder他是继承自NSObject,所以目前的继承关系NSObject--> UIResponder--> UIView

    下面一张图简单说下开发中经常用的控件继承关系

    iOS控件继承关系.jpg

    上图可以看到直接继承自UIView的为左侧部分,简介继承的为中间及右侧部分,子类拥有父类的方法以及属性
    1.UIScrollView的子类一般都可以滑动

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;                                               // any offset changes
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes
    
    // called on start of dragging (may require some time and or distance to move)
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
    // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
    // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
    
    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   // called on finger up as we are moving
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt
    
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
    
    - (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;     // return a view that will be scaled. if delegate returns nil, nothing happens
    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content
    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations
    
    - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   // return a yes if you want to scroll to the top. if not defined, assumes YES
    - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      // called when scrolling animation finished. may be called immediately if already at top
    
    /* Also see -[UIScrollView adjustedContentInsetDidChange]
     */
    - (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView API_AVAILABLE(ios(11.0), tvos(11.0));
    

    2.UIControl的子类一般都可以添加监听点击的事件

    // add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
    // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
    // the action cannot be NULL. Note that the target is not retained.
    - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    

    具体用到的控件可以查看其相关的API,注释都是比较好懂的,很基础的东西,希望可以帮到小白吧,毕竟我也是个小白 。。。。

    相关文章

      网友评论

        本文标题:iOS 常用控件的继承关系

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