SCLAlertViewSCLAlertView: 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 画图;
2 _5 Z. P5 a- |7 s9 c, z9 n7 ~ 链式语法block 要玩的比较溜,
9 H4 o. ?+ K4 a4 A! ]6 b% f4 `% h - @property(copy, nonatomic) SCLAlertViewBuilder *(^cornerRadius) (CGFloat cornerRadius);& E/ a. E# |0 g7 M0 s& k0 Q" r
- - (SCLAlertViewBuilder *(^) (CGFloat cornerRadius))cornerRadius {! D) V. U/ ~: k" F: r) s
- if (!_cornerRadius) {
0 E; c7 [/ m5 D - __weak typeof(self) weakSelf = self;
4 \" `7 h' K% Y - _cornerRadius = ^(CGFloat cornerRadius) {
8 A( m9 [- X& l' y9 n' Y5 f: k - weakSelf.alertView.cornerRadius = cornerRadius;+ m( [& h% l. D4 L* I L
- return weakSelf;% z7 q- L( ~* _3 U7 w& n
- };
7 \' a) `0 c+ F2 P - }
7 Q* ~. I# F( S! W2 { - return _cornerRadius;
: H. E1 ^; C6 M: \0 L - }
- q+ ?; z; m: k - // 由于上面的属性名和入参名一样 所以我把入参名改为 value 了。
( B7 l$ k9 y# n0 H% X0 D7 m - @property(copy, nonatomic) SCLAlertViewBuilder *(^cornerRadius) (CGFloat value);
8 p1 [ J7 A1 G% J5 w% L" `( b - - (SCLAlertViewBuilder *(^) (CGFloat value))cornerRadius {
# H5 C7 V; _3 d i; d, m( o+ r& Q - if (!_cornerRadius) {
) ?4 e) l G4 Z% [ - __weak typeof(self) weakSelf = self;) P% F$ o% R; Y) S9 M
- _cornerRadius = ^(CGFloat value) {
3 i5 ^- e: A* Q+ h }5 F2 O - weakSelf.alertView.cornerRadius = value;& g9 h+ M7 l. p) A+ V( g9 n
- return weakSelf;& H* T9 r1 ~; z: {2 J
- };
0 c' J3 E" D# ~- c# Y" r/ f - }
$ `- y* |* c O3 p% l+ B X - return _cornerRadius;
T' S( K1 s$ t' v; V - }
复制代码 ! s% p; K C* z- j
该 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 各种类型所代码的图片。 4 I9 E7 B; K1 V
|