From 2d0570c09fae90d8ac38d452b1456c2856300475 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 20 Nov 2019 18:15:04 +0800 Subject: [PATCH] feat:iOS support toast gravity --- demo/src/ModalDemo.ts | 27 ++++++++++++++++++++++- iOS/Pod/Classes/Plugin/DoricModalPlugin.m | 7 +++++- iOS/Pod/Classes/Util/DoricUtil.h | 6 ++--- iOS/Pod/Classes/Util/DoricUtil.m | 20 ++++++++--------- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/demo/src/ModalDemo.ts b/demo/src/ModalDemo.ts index 71d9a06e..2ffb536f 100644 --- a/demo/src/ModalDemo.ts +++ b/demo/src/ModalDemo.ts @@ -14,7 +14,7 @@ class ModalDemo extends Panel { textAlignment: Gravity.Center, height: 50, }), - label('toast'), + label('toast on bottom'), label('Click me').apply({ width: 200, height: 50, @@ -26,6 +26,31 @@ class ModalDemo extends Panel { modal(context).toast('This is a toast.') } } as IText), + label('toast on top'), + label('Click me').apply({ + width: 200, + height: 50, + bgColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().exactly(), + onClick: () => { + modal(context).toast('This is a toast.', Gravity.Top) + } + } as IText), + + label('toast on center'), + label('Click me').apply({ + width: 200, + height: 50, + bgColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().exactly(), + onClick: () => { + modal(context).toast('This is a toast.', Gravity.Center) + } + } as IText), ]).apply({ layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT), gravity: Gravity.Center, diff --git a/iOS/Pod/Classes/Plugin/DoricModalPlugin.m b/iOS/Pod/Classes/Plugin/DoricModalPlugin.m index c92fd70a..b13f2734 100644 --- a/iOS/Pod/Classes/Plugin/DoricModalPlugin.m +++ b/iOS/Pod/Classes/Plugin/DoricModalPlugin.m @@ -20,6 +20,7 @@ // Created by pengfei.zhou on 2019/7/29. // +#import #import "DoricModalPlugin.h" #import "DoricUtil.h" @@ -27,7 +28,11 @@ @implementation DoricModalPlugin - (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise { dispatch_async(dispatch_get_main_queue(), ^{ - showToast(dic[@"msg"]); + __block DoricGravity gravity = BOTTOM; + [dic[@"gravity"] also:^(NSNumber *it) { + gravity = (DoricGravity) [it integerValue]; + }]; + showToast(dic[@"msg"], gravity); }); } diff --git a/iOS/Pod/Classes/Util/DoricUtil.h b/iOS/Pod/Classes/Util/DoricUtil.h index 408367c7..8931970d 100644 --- a/iOS/Pod/Classes/Util/DoricUtil.h +++ b/iOS/Pod/Classes/Util/DoricUtil.h @@ -21,6 +21,7 @@ // #import +#import "DoricLayouts.h" void DoricLog(NSString *_Nonnull format, ...); @@ -36,7 +37,4 @@ NSBundle *_Nonnull DoricBundle(void); #define DC_UNLOCK(lock) dispatch_semaphore_signal(lock); #endif -void showToastInView(NSString *_Nonnull text, UIView *_Nonnull superView); - - -void showToast(NSString *_Nonnull text); \ No newline at end of file +void showToast(NSString *_Nonnull text, DoricGravity gravity); \ No newline at end of file diff --git a/iOS/Pod/Classes/Util/DoricUtil.m b/iOS/Pod/Classes/Util/DoricUtil.m index 450a6212..5d6f5cc2 100644 --- a/iOS/Pod/Classes/Util/DoricUtil.m +++ b/iOS/Pod/Classes/Util/DoricUtil.m @@ -48,10 +48,8 @@ void DoricLog(NSString *_Nonnull format, ...) { } -void showToastInView(NSString *text, UIView *superView) { - if (!superView) { - return; - } +void showToast(NSString *text, DoricGravity gravity) { + UIView *superView = [UIApplication sharedApplication].windows.lastObject; UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:20.f]; label.text = text; @@ -63,15 +61,17 @@ void showToastInView(NSString *text, UIView *superView) { label.width += 30; label.height += 10; label.layer.cornerRadius = label.height / 2; - label.bottom = superView.height - 20; label.centerX = superView.width / 2; + if ((gravity & BOTTOM) == BOTTOM) { + label.bottom = superView.height - 20; + } else if ((gravity & TOP) == TOP) { + label.top = 108; + } else { + label.centerY = (superView.height - 88) / 2; + } + [superView addSubview:label]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [label removeFromSuperview]; }); -} - - -void showToast(NSString *text) { - showToastInView(text, [UIApplication sharedApplication].windows.lastObject); } \ No newline at end of file