ios重用机制_iOS可重用的弹出窗口和警报演示

ios重用机制

Picture yourself starting a new project and requirements coming up to make some small window sliding from bottom.

想像一下您自己开始一个新项目,并提出一些要求,以使一些小的窗口从底部滑动。

Piece of cake, right? We can’t use UIAlertController because it has to be fully customizable. We can’t use 3rd party libs because they are always limiting us in specific ways. So maybe we’ll make some base class that has the show and dismiss methods with sliding animation. If you think this solution is trivial, think again. Some developers would create a view, constraints, animations, buttons actions, all inside view controller. This view has zero reusability or maintainability.

小菜一碟吧? 我们不能使用UIAlertController,因为它必须是完全可定制的。 我们不能使用第三方库,因为它们总是以特定方式限制我们。 因此,也许我们将创建一些具有show和dismiss方法以及滑动动画的基类。 如果您认为此解决方案微不足道,请再考虑一下。 一些开发人员会在视图控制器内部创建视图,约束,动画,按钮动作。 该视图的可重用性或可维护性为零。

Then you overhear designer saying we will also be needing different sized popups, with other animations. Some will have background fading presentation. This background can block user taps and optionally close the popup. Others will be small, more like a tooltip.

然后,您偷听设计师说我们还将需要带有其他动画的不同大小的弹出窗口。 有些将具有背景褪色演示。 此背景可以阻止用户点击并有选择地关闭弹出窗口。 其他的则很小,更像一个工具提示。

Image for post

Fast forward a couple of months into the future, and you have tons of conditions inside the base class and three layers of subclasses of your popups. How to handle these cases with minimal effort and maximal flexibility?

快进未来几个月,您将在基础类和弹出窗口的三层子类中拥有大量条件。 如何以最小的努力和最大的灵活性来处理这些情况?

There are two types of popups to my knowledge. Background interaction blocking and not. In this article, I will show a way to isolate and reuse their presentation logic.

据我所知,有两种弹出窗口。 后台交互没有阻塞。 在本文中,我将展示一种隔离和重用其表示逻辑的方法。

背景阻止弹出窗口。 (Background blocking popups.)

UIKit has given us the best way to handle these popups, and it’s called UIViewControllerTransitioningDelegate. It has a few parts, but once you write it, you shorten your development time. Peculiarly, I didn’t encounter tutorials that explain how to use this for everyday and straightforward things like popups.

UIKit为我们提供了处理这些弹出窗口的最佳方法,称为UIViewControllerTransitioningDelegate 。 它由几部分组成,但是一旦编写,就可以缩短开发时间。 奇特的是,我没有遇到教程来解释如何将其用于日常和简单的事情(如弹出窗口)。

Let’s start with PopupPresentationManager, subclass of UIViewControllerTransitioningDelegate. We can make this delegate a property of view controller, which allows us to avoid duplicating conformance methods in other view controllers.

让我们从PopupPresentationManager(UIViewControllerTransitioningDelegate的子类)开始。 我们可以使该委托成为视图控制器的属性,这可以避免在其他视图控制器中重复使用一致性方法。

It has two parts. 1. UIPresentationController: responsible for size and origin of the view. And also for dimming view presentation. 2. UIViewControllerAnimatedTransitioning: does main animation.

它分为两个部分。 1. UIPresentationController:负责视图的大小和原点。 并用于调暗视图演示。 2. UIViewControllerAnimatedTransitioning:做主动画。

Before we talk about these heavier parts, let’s look how we would create and use popup like that. Create UnicornPopup, subclass of UIViewController. Also make nib with same name and owner. Don’t forget to set that owner view property.

在讨论这些较重的部分之前,让我们看一下如何创建和使用这样的弹出窗口。 创建UnicornPopup,UIViewController的子类。 还要使笔尖具有相同的名称和所有者。 不要忘记设置所有者视图属性。

To present it, we need to simply set its transitioningDelegate.

要展示它,我们只需要简单地设置它的transitioningDelegate。

let popup = UnicornPopup()popup.transitioningDelegate = popup.presentationManagerpopup.modalPresentationStyle = .custompresent(popup, animated: true)

You can see how we can reuse this presentationManager in another similar popups.

您可以看到我们如何在另一个类似的弹出窗口中重用此presentationManager。

Now let’s create DimmedPopupPresentationController, subclass of UIPresentationController. It will add dimming view when presentation of view controller begins.

现在让我们创建DimmedPopupPresentationController,它是UIPresentationController的子类。 当视图控制器的演示开始时,它将添加变暗视图。

We said that it determines size and origin of popup. But how can view controller pass it? One way is through argument in initializer. Another is to use preferredContentSize property, which every view controller has. All what is left for popup is to tell UIPresentationController how much size should it occupy on the screen. Also we need to take care of user taps on background dimming view. Sometimes we want it to dismiss the popup and sometime we don’t. Let’s create DimmingViewTappedProtocol. If view controller conforms to it, then it will have this kind of behaviour.

我们说过,它决定了弹出窗口的大小和来源。 但是视图控制器如何通过它呢? 一种方法是通过初始化程序中的参数。 另一个方法是使用每个视图控制器都具有的preferredContentSize属性。 弹出窗口剩下的就是告诉UIPresentationController它应该在屏幕上占据多少大小。 另外,我们还需要注意背景调光视图上的用户点击。 有时我们希望它消除弹出窗口,有时我们不这样做。 让我们创建DimmingViewTappedProtocol。 如果视图控制器符合它,那么它将具有这种行为。

Let’s update UnicornPopup with DimmingViewTappedProtocol conformance and set preferredContentSize.

让我们使用DimmingViewTappedProtocol一致性更新UnicornPopup并设置preferredContentSize。

All right! To the last part. SlideInPresentationAnimator, which applies animation on frame property of presented/dismissed view controller. You can get very creative with animations. Change alpha, transforms, scale. Or simulate transition of views from one screen to another. It’s up to you.

行! 到最后一部分。 SlideInPresentationAnimator,将动画应用于显示/关闭视图控制器的frame属性。 您可以通过动画获得很大的创造力。 更改Alpha,变换,缩放。 或模拟视图从一个屏幕到另一个屏幕的过渡。 由你决定。

Wow, that’s it? Yes! We moved presentation logic outside of the view. And with that we now can switch different parts like popup size, origin and animations without rewriting all the logic from scratch. We can even use this popup as full sized view controller because its presentation is part of UIKit flow.

哇,就是这样吗? 是! 我们将表示逻辑移到了视图之外。 这样,我们现在可以切换不同的部分,如弹出窗口大小,原始和动画,而无需从头开始重写所有逻辑。 我们甚至可以将此弹出窗口用作全尺寸视图控制器,因为其表示形式是UIKit流的一部分。

And the result.

结果。

Image for post

非阻塞警报 (Non-blocking alerts)

To show non blocking view above the view controller we can add subview or add view controller as a child. I prefer to work with child view controllers because their life cycle is more convenient, in my opinion.

为了在视图控制器上方显示非阻塞视图,我们可以添加子视图或将视图控制器添加为子视图。 我更喜欢与儿童视图控制器一起工作,因为我认为它们的生命周期更方便。

Typically you would subclass base class to reuse the show and dismiss methods. It aint much but it’s honest work. I like to avoid subclassing when possible. Base classes usually end up as a pile of lots of unnecessary variables and conditions. So by avoiding subclassing you make it more maintainable. Let’s use protocol AlertPopupBase. Conforming view controller can pass its size to the show method by implementing alertFrame.

通常,您可以对基类进行子类化,以重用show和dismiss方法。 虽然不是很多,但这是诚实的工作。 我希望尽可能避免子类化。 基类通常以大量不必要的变量和条件结束。 因此,通过避免子类化,可以使其更易于维护。 让我们使用协议AlertPopupBase。 一致性视图控制器可以通过实现alertFrame将其大小传递给show方法。

Let’s implement SexyFishAlert conforming to AlertPopupBase. Like in previous example, create nib, set its owner to SexyFishAlert and define view and outlets.

让我们实现符合AlertPopupBase的SexyFishAlert。 像前面的示例一样,创建笔尖,将其所有者设置为SexyFishAlert并定义视图和出口。

Usage:

用法:

//make reference in view controller
var
sexyFishAlert: SexyFishAlert?
sexyFishAlert = SexyFishAlert()sexyFishAlert?.show(in: self, title: "Hi darling, I am a fish")
Image for post

结论 (Conclusion)

I am sure these solutions are not suitable for every possible scenario. This is not the point. But rather to cover your most common needs and stay flexible at the same time.

我确信这些解决方案并不适合所有可能的情况。 这不是重点。 而是要满足您最常见的需求并同时保持灵活性。

翻译自: https://medium.com/swlh/ios-reusable-popups-and-alerts-presentation-82fc1c6aef75

ios重用机制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值