swift 创建类扩展_在Swift中创建一个可扩展按钮

swift 创建类扩展

We’re going to build a custom expandable button in Swift, like the one below:

我们将在Swift中构建一个自定义的可扩展按钮,如下所示:

First off, it’s important to understand the view structure that is necessary to implement a component like this one.

首先,重要的是要了解实现这样的组件所必需的视图结构。

We will create a subclass of UIView that will be composed of 5 subviews like so:

我们将创建UIView的子类,该子类将由5个子视图组成,如下所示:

  1. A containerStackView that will hold the menuButton and the expandedStackView

    一个containerStackView ,将保存menuButtonexpandedStackView

  2. A menuButton that, when tapped, will handle the expanding animation

    一个menuButton ,在点击时将处理扩展的动画

  3. An expandedStackView that will be shown or hidden when the menuButton is tapped

    一个expandedStackView将被显示时,或者当隐藏menuButton被点击

  4. A dogButton that will be part of the expandedStackView

    一个dogButton将成为一部分expandedStackView

  5. A catButton that will also be part of the expandedStackView

    一个catButton这也将是部分expandedStackView

Button Panel View Structure
Button Panel View Structure. The arrow signals the stack view that will be shown and hidden to emulate the expand and collapse animations, respectively. 按钮面板视图结构。 箭头表示将分别显示和隐藏以模拟展开和折叠动画的堆栈视图。

创建我们的自定义UIView (Creating our custom UIView)

Let’s start by setting up our view, which we will call ButtonPanelView. We will first create 3 instances of UIButton and 2 instances of UIStackView.

让我们从设置视图开始,我们将其称为ButtonPanelView 。 我们将首先创建3个UIButton实例和2个UIStackView实例。

We will add dogButton and catButton as arranged subviews of expandedStackView. We will then add expandedStackView and menuButton as arranged subviews of containerStackView.

我们将增加dogButtoncatButton作为安排子视图expandedStackView 。 然后,我们将添加expandedStackViewmenuButton的排列子视图containerStackView

We can do some view styling (e.g. add color and shadows) as well as add any necessary constraints when the ButtonPanelView is initialized.

在初始化ButtonPanelView时,我们可以进行一些视图样式设置(例如,添加颜色和阴影)以及添加任何必要的约束。

动画 (Animation)

The animation itself is very simple; every time the menu button is tapped, we will flip the isHidden state of the expandedStackView’s subviews (i.e. dogButton and catButton) and of the expandedStackView itself, using UIKit’s UIView.animate method.

动画本身非常简单。 每一个菜单按钮被点击的时候,我们会翻转isHidden的状态expandedStackView的子视图(即dogButtoncatButton )和中expandedStackView本身,使用UIKitUIView.animate方法。

We can also make some simple modifications to our animation code in handleTogglePanelButtonTapped so that the ➕ emoji changes to ❌ when the panel is expanded, and vice versa when collapsed.

我们还可以在handleTogglePanelButtonTapped对动画代码进行一些简单的修改,以便在扩展面板时将➕表情符号更改为❌,而在折叠时则相反。

Image for post
Expandable button
展开式按钮

Here’s the final implementation of our ButtonPanelView class:

这是我们的ButtonPanelView类的最终实现:

Thanks for reading! You can find a full demo of the expandable button on GitHub.

谢谢阅读! 您可以在GitHub找到可扩展按钮的完整演示。

翻译自: https://medium.com/swlh/creating-an-expandable-button-in-swift-8368165b999e

swift 创建类扩展

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值