ios自定义动画帧的hud_在SwiftUI中创建自定义信息HUD

ios自定义动画帧的hud

With iOS 13 Apple changed from a big, disruptive UI for silent mode and volume to smaller and more focused UIs, that don’t take up the whole screen. There are buttons in your app, where a feedback is more intuitive for the user, but not so important to disrupt the whole screen with an ActionSheet . Examples are “Added to favorites” or “Saved image”. For that, a simple info sheet at the top is great.

随着iOS 13的发布,苹果公司从用于破坏性模式和音量的大型破坏性UI变成了无法占据整个屏幕的更小且更具针对性的UI。 应用程序中有一些按钮,对于用户来说,反馈更直观,但使用ActionSheet破坏整个屏幕并不是那么重要。 例如“添加到收藏夹”或“保存的图像”。 为此,顶部的简单信息表很棒。

建立您的快速信息表 (Building your quick info sheet)

The HUD will be displayed in a layer above the main app interface. To do so, wrap the view elements in your ContentView in a ZStack . Than we can add the HUD in the ZStack . First, we create an simple HUD with only a Text as content. The modifiers create the iOS-HUD style. As background we use a blur UIViewRepresentable , but you can also use Color(.systemBackground) or any other color.

HUD将显示在主应用程序界面上方的图层中。 为此,将View元素包装在ZStackContentView中。 比我们可以在ZStack添加HUD。 首先,我们创建一个仅包含Text作为内容的简单HUD。 修改器创建iOS-HUD样式。 作为背景,我们使用模糊UIViewRepresentable ,但是您也可以使用Color(.systemBackground)或任何其他颜色。

Image for post

This looks great! When we put the HUD() in our ContentView , the HUD is in the middle of the view, instead of the top. Simply set the alignment of the ZStack to .top , and the OS does the rest. Via the safe margins, the HUD is always placed right.

看起来很棒! 当将HUD()放入ContentView ,HUD位于视图的中间,而不是顶部。 只需将ZStackalignment设置为.top ,剩下的就由操作系统完成。 通过安全边距,HUD始终放置在正确的位置。

使HUD动画化 (Animating the HUD)

We want the HUD to be hidden by default, and moves from the top when we tap the Button .

我们希望HUD默认处于隐藏状态,并在单击Button时从顶部移动。

Image for post

The animation is beautiful, but we want the button to only activate the HUD(), and that the HUD dismisses itself after time.

动画很漂亮,但是我们希望按钮仅激活HUD() ,并且HUD会在一段时间后关闭。

With the function added, the button only shows the HUD, and it dismisses itself after 2 seconds. Depending on what content you display, you can change the time in the dismissHUDAfterTime() method.

添加该功能后,该按钮仅显示HUD,并在2秒钟后关闭。 根据显示的内容,您可以在dismissHUDAfterTime()方法中更改时间。

更改HUD的内容 (Change the HUD’s content)

The HUD should only contain limited content. To amplify the message, add a system image to the HUD.

HUD应该只包含有限的内容。 要放大消息,请向HUD添加系统映像。

Change the Text to a Label and the HUD is instantly much more appealing and information rich. If you want to know more about LabelStyles , check my article about this topic out.

Text更改为Label ,HUD会立即变得更具吸引力且信息丰富。 如果您想了解有关LabelStyles更多信息,请查看有关此主题的文章

Image for post

向HUD添加动作 (Adding actions to the HUD)

The HUD shouldn’t be used to add actions, but simple functionality can be very useful. For example when tapping the HUD, it dismisses itself.

HUD不应用于添加动作,但是简单的功能可能非常有用。 例如,在点击HUD时,它会自动关闭。

Or you can add message specific action like opening the Photos app.

或者,您可以添加特定于消息的操作,例如打开“照片”应用程序。

在详细视图中使用HUD (Using the HUD in a detail view)

When you use the HUD in a detail view, and not in the main view, you should still put the main view in the ZStack . Then add an @Binding and the Button in the detail view and it works perfectly fine.

当在详细视图中而不是在主视图中使用HUD时,仍应将主视图放在ZStack 。 然后在详细信息视图中添加一个@BindingButton ,它可以很好地工作。

更多想法 (More ideas)

You have limited, but still many options on what to display in the HUD. For example, when you have a Slider or Picker to change the brightness in your app, you could display the brightness value in a HUD.

您在HUD中显示的内容有限,但仍有很多选择。 例如,当您使用SliderPicker更改应用程序中的亮度时,可以在HUD中显示亮度值。

Image for post

The same approach can be used, when you have a custom volume control. Together with .matchedGeometryEffect() you can create great transitions like this:

当您具有自定义音量控件时,可以使用相同的方法。 与.matchedGeometryEffect()一起,您可以创建出色的过渡效果,如下所示:

Image for post

Although that looks complex, this is very very easy and needs only some lines of code.

尽管看起来很复杂,但这非常简单,只需要几行代码。

The most is only styling, but the important parts are the @Namespace , the .matchedGeometryEffect() modifiers and the if showHUD {} .

最多的是只造型,但重要的部分是@Namespace.matchedGeometryEffect()修饰符和if showHUD {}

结论 (Conclusion)

When you want to give the user quick feedback, that an action happened, HUDs are the ideal element for you. They are not as interruptive than ActionSheets but can display much information with less content.

当您想给用户快速反馈,说明有动作发生时,HUD是您的理想选择。 它们不像ActionSheets那样具有干扰ActionSheets但是可以显示较少内容的大量信息。

翻译自: https://itnext.io/create-custom-info-huds-in-swiftui-a3c2baabbddb

ios自定义动画帧的hud

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XNProgressHUD (https://github.com/LuohanCC/XNProgressHUD) 一款支持支持自定义的轻量级HUD,支持垂直、水平两种样式。SVProgressHUD非常灵活,所见的部分都可根据自己的要求进行自定义,包括自义动画效果或图片,只需要实现相关协议方法。 安装使用 pod 'XNProgressHUD' 使用说明 在Window上显示: [XNHUD showLoadingWithTitle:@"正在登录"]; [XNHUD showWithTitle:@"这是一个支持自定义的轻量级HUD"]; [XNHUD showInfoWithTitle:@"邮箱地址不能为空"]; [XNHUD showErrorWithTitle:@"拒绝访问"]; [XNHUD showSuccessWithTitle:@"操作成功"]; 在ViewController上显示(maskType.enable=true时,导航栏依然可以接受点击事件) // 引入'UIViewController XNProgressHUD.h' [self.hud showLoadingWithTitle:@"正在登录"]; [self.hud showWithTitle:@"这是一个支持自定义的轻量级HUD"]; [self.hud showInfoWithTitle:@"邮箱地址不能为空"]; [self.hud showErrorWithTitle:@"拒绝访问"]; [self.hud showSuccessWithTitle:@"操作成功"]; // 设置显示位置 [XNHUD setPosition:CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height * 0.7)]; // 设置主色调 [XNHUD setTintColor:[UIColor colorWithRed:38/255.0 green:50/255.0 blue:56/255.0 alpha:0.8]]; // 设置相应的maskType转态下的颜色(16进制颜色值) [XNHUD setMaskType:(XNProgressHUDMaskTypeBlack) hexColor:0x00000044]; [XNHUD setMaskType:(XNProgressHUDMaskTypeCustom) hexColor:0xff000044]; 属性和方法说明 显示时长minimumDelayDismissDuration作用于非加载样式的视图:XNRefreshViewStyleInfoImage、XNRefreshViewStyleError、XNRefreshViewStyleSuccess; 显示时长maximumDelayDismissDuration作用与加载样式的视图:XNRefreshViewStyleLoading、XNRefreshViewStyleProgress。 @property (nonatomic, assign) NSTimeInterval minimumDelayDismissDuration; //default:1.5f @property (nonatomic, assign) NSTimeInterval maximumDelayDismissDuration; //default:20.f 延时显示时间和延时消失时间,该方法只对下一次HUD显示生效(只生效一次)。 [XNHUD setDisposableDelayResponse:1.0f delayDismiss:2.0f]; 设置排列方向,默认为水平方向 [XNHUD setOrientation:XNProgressHUDOrientationHorizontal]; 自定义XNProgressHUD 如果需要自定义加载视图的显示内容和动画样式,请重写XNRefreshView并实现XNRefreshViewProtocol的协议方法即可,具体如下 1.自定义XNRefreshView并实现XNRefreshViewProtocol的协议方法; 2.继承XNProgressHUD并实现XNProgressHUDMethod的协议方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值