用户
 找回密码
 立即注册

发帖

SCLAlertView 源码解析

[复制链接]
  • TA的每日心情
    慵懒
    2025-11-24 10:46
  • 27

    主题

    6

    回帖

    669

    积分

    管理员

    积分
    669
    发表于 2017-1-20 09:25:56
    SCLAlertView
    SCLAlertView: Beautiful animated Alert View. Swift version. 它是继承自 UIViewController,而不是UIView。
    逻辑整理
    上面的圆圈图标由 circleViewBackground -> circleView -> circleIconImageView 层级构成,由 layer.cornerRadius 指定它们的圆圈角度。
    最下层全屏幕显示的背景 backgroundView 有三种样式,阴影、模糊(由苹果 UIImage+ImageEffects 文件处理)、透明。
    alertView 需要展示内容添加到 contentView 上,用一个数组 customViews 来装这些自定义的 view,目的是为了调整 alertview 的 frame。
    而 textfield 则是用 inputs 数组来装,要让不是最后一个的 textfield.returnKeyType 为 UIReturnKeyNext,这样用户点击键盘上的完成后,焦点自动定位到下一个 textfield。 并且在 textFieldShouldReturn: 代理里面 becomeFirstResponder 弹出键盘。
    它的显示方式有两种,一种是显示在某个 parentViewController 上(addChildViewController,另外一种就是添加到自定义的 Window 上(设置 windowLevel、rootViewController)。
    知识点
    • 链式语法;
    • UIView 动画的使用;
    • tintColor 知识点;
    • UIWindow 简易知识点;
    • UIControl sendAction: ;
    • UIBezierPath 画图;
        T. m# T  M& u
    链式语法
    block 要玩的比较溜,. w6 q+ o+ s! N
    1. @property(copy, nonatomic) SCLAlertViewBuilder *(^cornerRadius) (CGFloat cornerRadius);; E) B3 P! N* i' W+ u
    2. - (SCLAlertViewBuilder *(^) (CGFloat cornerRadius))cornerRadius {
      2 W! Y$ N5 _: h: x
    3.     if (!_cornerRadius) {9 v5 d2 i/ l* D' g3 C
    4.         __weak typeof(self) weakSelf = self;  d8 m# L- E) R- F' v4 n7 Y
    5.         _cornerRadius = ^(CGFloat cornerRadius) {3 u* A* O" `$ @7 U' F6 X
    6.             weakSelf.alertView.cornerRadius = cornerRadius;
      & K. E/ V2 h: @8 f3 u6 ^) `
    7.             return weakSelf;0 P" M- g2 |& H  m( q" u
    8.         };
      4 `& O: I7 @+ R  b2 x- f
    9.     }
      6 ~# z" N7 T$ p
    10.     return _cornerRadius;
      ' c; z- P, H4 k* a
    11. }
      & {2 [. y# d- d7 i
    12. // 由于上面的属性名和入参名一样 所以我把入参名改为 value 了。. v0 Z6 u- b; o% q* D+ B
    13. @property(copy, nonatomic) SCLAlertViewBuilder *(^cornerRadius) (CGFloat value);$ C3 G2 v1 {" T
    14. - (SCLAlertViewBuilder *(^) (CGFloat value))cornerRadius {
      & x+ }1 B+ c  |: z9 R, U3 W, s
    15.     if (!_cornerRadius) {: w& A) a7 Q& b
    16.         __weak typeof(self) weakSelf = self;4 {( P8 G1 j; U
    17.         _cornerRadius = ^(CGFloat value) {3 o0 S$ h$ p' k: J4 c
    18.             weakSelf.alertView.cornerRadius = value;
      5 d% C2 \0 T) g& ^
    19.             return weakSelf;
      + X* `& n: n2 c2 P+ y7 w
    20.         };9 [. f" ^8 A" r/ h
    21.     }
      ! G$ H* I8 {  O1 S. v5 w: h
    22.     return _cornerRadius;8 |, i0 S/ I+ a- B" Y) W
    23. }
    复制代码
    * }6 N0 s% I9 z$ s- E9 y. Z
    该 block 的返回值类型就是自己,入参就是你要设置的属性。本来想写个 UIView 的相关设置来练下手的,结果发现已经有人写了。
    UIView 动画的使用
    根据 SCLAlertViewShowAnimation 和 SCLAlertViewHideAnimation 来调用不同的动画,而这个动画都是由 UIView 的类方法完成的。
    tintColor 知识点
    tintColor 永远会返回一个 color 值(默认为 UIDeviceRGBColorSpace 0 0.478431 1 1)。当父视图的 tintColor 发生改变时,它所有的子视图的 tintColor 也会跟着发生改变。我们可以重载 tintColorDidChange 发生来监听 color 的改变,来调整自身的颜色。
    UIWindow 简易知识点
    windowLevel、rootViewController、makeKeyAndVisible 等。
    UIControl sendAction:
    事件的传递。
    UIBezierPath 画图
    在 SCLAlertViewStyleKit 类里面,用 UIBezierPath 画 SCLAlertViewStyle 各种类型所代码的图片。
    ! q! v2 ]' s. \0 W% O' L$ k
    使用道具 举报 回复
    严禁恶意灌水!!!拒绝伸手党!!!
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    ض