UIControl笔记

  1. UIControl继承自UIView
  2. UIControl与Target-Action模式
    使用addTarget:action:forControlEvents方法来设置某一个controlEvent对应的方法(action)及要相应这个事件的target。

    target一般是包含uicontrol的rootview对应的vc。
    action有以下三种类型。其中sender对应uicontrol,event对应相应的事件

    - (IBAction)doSomething;
    - (IBAction)doSomething:(id)sender;
    - (IBAction)doSomething:(id)sender forEvent:(UIEvent*)event;
    
  3. UIControl的State
    UIControl可以同时处于两个状态。链接

    • UIControlStateNormal 默认状态
    • UIControlStateHighlighted 触摸事件发生在UIControl内部时,变为高亮态。当在内部touchup或在触摸时间发生在外部时,失去高亮态。是一种非持久态
    • UIControlStateDisabled 禁止态,不处理触摸事件
    • UIControlStateSelected 控件被选中,可以持久存在。
    • UIControlStateFocused
  4. 添加target-action

    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    
    • target 要被作用的target,即这个target对应的action要被调用。若为nil,那么系统会顺着响应链寻找。
    • action 要被调用的方法,不能是nil。
    • controlEvents 可以包含多个controlEvent,即可以同时为多个controlEvent指定事件 。
  5. 移除target-action

  6. - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    
    • target 要被移除的target,若为nil,哪么移除对应controlEvent的所有target
    • action 要被移除的action。
  7. 人为触发事件

    • - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event; 对指定的target调用指定的action。event可以为nil。
    • - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents; 遍历这个UIControl的所有target,若对controlEvent指定了对应的action,调用上一个方法。
  8. 状态循环
    一般如果是由Touch Up Inside触发的事件:

    • 手指按到控件,UIControl更新ControlState到Highlighted
    • 手指离开控件,一般UIControl更新ControlState到Selected
    • Highlighted -> Selected之后的状态取决于具体的UIControl子类,其实连UITableViewCell也有这三种状态,Cell还是UIView的子类,这种特殊的状态默认就不会更新回Normal了~
    • 至于在UIbutton中,那就是非常自然的时序:Normal -> Highlighted -> Selected -> Normal啦。

链接:https://www.zhihu.com/question/25284619/answer/30370393

posted on 2017-01-14 08:44  花老🐯  阅读(329)  评论(0编辑  收藏  举报

导航