当前位置: 首页 > news >正文

用自己电脑做服务器 网站百度手机助手下载安卓版

用自己电脑做服务器 网站,百度手机助手下载安卓版,品牌vi设计是什么,最新新闻热点事件2021年7月概览 在 SwiftUI 的开发过程中我们常说:“屏幕不够,滚动来凑”。可见滚动视图对于超长内容的呈现有着多么秉轴持钧的重要作用。 这不,从 SwiftUI 5.0(iOS 17)开始苹果又为滚动视图增加了全新的功能。但是官方的示例可…

在这里插入图片描述

概览

在 SwiftUI 的开发过程中我们常说:“屏幕不够,滚动来凑”。可见滚动视图对于超长内容的呈现有着多么秉轴持钧的重要作用。

在这里插入图片描述

这不,从 SwiftUI 5.0(iOS 17)开始苹果又为滚动视图增加了全新的功能。但是官方的示例可能会让小伙伴们“雾里看花”、不求甚解。所以,本篇博文存在的真谛就尽在于此了!

在本篇博文中,您将学到如下内容:

  • 概览
  • 1. 什么是滚动目标行为(Scroll Target Behavior)?
  • 2. scrollTargetLayout 视图修改器到底是干嘛用的?
  • 3. 定制我们自己的 ScrollTargetBehavior 滚动目标行为
  • 总结

相信学完本课后,小伙伴们一定会对 SwiftUI 5.0 中新的 scrollTargetLayout 以及 scrollTargetBehavior 修改器的含义和使用“醍醐灌顶”、如梦初醒!

那还等什么呢?让我们马上进入滚动的世界吧!

Let’s rolling!!!😉


本文对应的视频课在此,欢迎小伙伴们恣意观赏:

SwiftUI 5.0 滚动视图的滚动目标行为解惑和实战


1. 什么是滚动目标行为(Scroll Target Behavior)?

从 SwiftUI 5.0 开始,苹果为滚动视图特地新增了 scrollTargetBehavior 修改器方法:

在这里插入图片描述

使用它我们可以根据滚动轴来设置滚动视图的滚动目标行为(Scroll Target Behavior)。那么,什么是滚动目标行为呢?简单来说,它表示滚动视图中的滚动目标(Scroll Targets)在滚动停止时以何种方式对齐。

import SwiftUIenum ScrollAlignType: Identifiable, CaseIterable {case none, paging, viewvar align: AnyScrollTargetBehavior {switch self {case .none:.init(.viewAligned(limitBehavior: .never))case .paging:.init(.paging)case .view:.init(.viewAligned)}}var title: String {switch self {case .none:"无"case .paging:"按页面"case .view:"按视图"}}var id: Int {title.hashValue}
}struct ContentView: View {@State private var scrollAlignType = ScrollAlignType.nonevar body: some View {ScrollView(.vertical) {ForEach(1...100, id: \.self) { i inText("Item \(i)").font(.largeTitle.weight(.heavy)).foregroundStyle(.white).frame(width: 300, height: 200).background {Capsule().foregroundStyle(.blue.gradient)}}}.scrollTargetBehavior(scrollAlignType.align).padding(.vertical, 20.0).ignoresSafeArea().safeAreaInset(edge: .top) {Picker("滚动目标行为", selection: $scrollAlignType) {ForEach(ScrollAlignType.allCases) { alignType inText(alignType.title).tag(alignType)}}.pickerStyle(.segmented).padding()}}
}#Preview {ContentView()
}

在上面的代码中,我们尝试用三种不同方式来对齐滚动视图中的滚动目标,它们分别是:

  1. 无(滚到哪是哪)
  2. 按页面对齐
  3. 按视图对齐

运行可以发现:前两种滚动对齐效果和我们的想象不谋而合,不过最后一种以视图为基准的对齐却貌似没起到什么作用,这是怎么回事呢?

在这里插入图片描述

2. scrollTargetLayout 视图修改器到底是干嘛用的?

原来,要想以滚动视图内部独立子元素为基准应用滚动目标行为,我们必须明确设置滚动目标(Scroll Targets),这是通过调用 scrollTargetLayout 视图修改器来实现的:

在这里插入图片描述

我们也可以理解为 scrollTargetLayout 方法将最外层的布局配置成了滚动目标布局。所以上面的代码我们需要做如下修正:

ScrollView(.vertical) {ForEach(1...100, id: \.self) { i inText("Item \(i)").font(.largeTitle.weight(.heavy)).foregroundStyle(.white).frame(width: 300, height: 200).background {Capsule().foregroundStyle(.blue.gradient)}}// 明确设置滚动目标.scrollTargetLayout()
}
.scrollTargetBehavior(scrollAlignType.align)
.padding(.vertical, 20.0)

重新运行可以看到,以视图为基准的滚动对齐已然生效了:

在这里插入图片描述

另外,如果滚动视图中动态生成的内容需要放在额外惰性容器(比如 LazyVStack 或 LazyHStack)中,我们需要在这些容器外层应用 scrollTargetLayout() 修改器方法:

ScrollView(.vertical) {LazyVStack {ForEach(1...100, id: \.self) { i inText("Item \(i)").font(.largeTitle.weight(.heavy)).foregroundStyle(.white).frame(width: 300, height: 200).background {Capsule().foregroundStyle(.blue.gradient)}}}.scrollTargetLayout()
}
.scrollTargetBehavior(scrollAlignType.align)
.padding(.vertical, 20.0)

有些小伙伴们可能会问:为什么要做这种看似“多此一举”的事呢?

考虑下面这个例子,我们不希望滚动目标行为应用在滚动的头和尾视图上,所以只要在中间滚动内容上启用 scrollTargetLayout 就“水到渠成”啦:

struct AnotherExampleScrollView: View {var body: some View {ScrollView {CustomHeaderView()LazyVStack {// 实际的滚动内容}.scrollTargetLayout()CustomFooterView()}.scrollTargetBehavior(.viewAligned)}
}

到目前为止(iOS 18 beta3),所有滚动目标行为相关的修改器方法都只能直接用在滚动视图(ScrollView)上,而不能用在 List 或 Form 这种内部“间接”使用滚动视图的容器上。

3. 定制我们自己的 ScrollTargetBehavior 滚动目标行为

除了使用 SwiftUI 系统默认的滚动目标行为(Scroll Target Behavior)以外,我们还可以按照实际需求创建特定的滚动对齐行为,这是通过遵循 ScrollTargetBehavior 协议来实现的:

在这里插入图片描述

遵循该协议只需完成一个 updateTarget 方法,在该方法传入的实参中我们可以根据当前滚动目标上下文(TargetContext)来恣意修改滚动目标(ScrollTarget)的位置等信息:

struct CustomScrollTargetBehavior: ScrollTargetBehavior {func updateTarget(_ target: inout ScrollTarget, context: TargetContext) {if context.velocity.dy > 0 {target.rect.origin.y = context.originalTarget.rect.maxY} else if context.velocity.dy < 0 {target.rect.origin.y = context.originalTarget.rect.minY}}
}extension ScrollTargetBehavior where Self == CustomScrollTargetBehavior {static var custom: CustomScrollTargetBehavior { .init() }
}

如上代码所示,我们创建了一个定制的 CustomScrollTargetBehavior 滚动目标行为,在其中:

  • 当滚动内容向上滚动时,context.velocity.dy 为正值;
  • 当滚动内容向下滚动时,context.velocity.dy 为负值;
  • 滚动速度越快,context.velocity.dy 绝对值越大;

从 updateTarget 的代码逻辑不难看到:我们自定义创建的这种新滚动模式,它在滚动时的阻尼感特别强。

现在,可以非常方便轻松的在滚动视图中应用我们自己的滚动目标行为啦:

ScrollView(.vertical) {LazyVStack {ForEach(1...100, id: \.self) { i inText("Item \(i)").font(.largeTitle.weight(.heavy)).foregroundStyle(.white).frame(width: 300, height: 200).background {Capsule().foregroundStyle(.blue.gradient)}}}.scrollTargetLayout()
}
.scrollTargetBehavior(.custom)
.padding(.vertical, 20.0)

最后运行一下代码,看看新的滚动效果吧:

在这里插入图片描述

利用 SwiftUI 5.0(iOS 17.0)中新的滚动目标行为机制,我们可以逍遥物外的自由定制滚动视图的滚动对齐模式啦!棒棒哒!💯

总结

在本篇博文中,我们讨论了什么是 SwiftUI 5.0(iOS 17.0)中新增的滚动目标行为(Target Behavior),并且介绍了如何游刃有余应用它们,我们在最后还创建了定制的滚动目标行为让自由度更加“出谷迁乔”。

感谢观赏,再会啦!😎


文章转载自:
http://dinncosmoothie.stkw.cn
http://dinncoshaker.stkw.cn
http://dinncobrimfull.stkw.cn
http://dinncotubule.stkw.cn
http://dinncobimane.stkw.cn
http://dinncogorilloid.stkw.cn
http://dinncoscourway.stkw.cn
http://dinncooveremployment.stkw.cn
http://dinncoconstrainedly.stkw.cn
http://dinncotranscript.stkw.cn
http://dinncopatsy.stkw.cn
http://dinncopantisocracy.stkw.cn
http://dinncobrisance.stkw.cn
http://dinncomatara.stkw.cn
http://dinncoschizanthus.stkw.cn
http://dinncofandom.stkw.cn
http://dinncorugose.stkw.cn
http://dinncocytopathologist.stkw.cn
http://dinncoras.stkw.cn
http://dinncoballast.stkw.cn
http://dinncoswash.stkw.cn
http://dinncorabbiteye.stkw.cn
http://dinncomussalman.stkw.cn
http://dinncoorientalize.stkw.cn
http://dinncodas.stkw.cn
http://dinncoenmity.stkw.cn
http://dinncoadhere.stkw.cn
http://dinncosubpolar.stkw.cn
http://dinncopiamater.stkw.cn
http://dinncoconcretist.stkw.cn
http://dinncofloc.stkw.cn
http://dinncopixel.stkw.cn
http://dinncotunic.stkw.cn
http://dinncowindsurf.stkw.cn
http://dinncoodontophore.stkw.cn
http://dinncolongness.stkw.cn
http://dinncowolfkin.stkw.cn
http://dinncodreamscape.stkw.cn
http://dinncoamidate.stkw.cn
http://dinncocircumcentre.stkw.cn
http://dinncobrimless.stkw.cn
http://dinncorhonda.stkw.cn
http://dinncohandclasp.stkw.cn
http://dinncohydrocoral.stkw.cn
http://dinnconovennial.stkw.cn
http://dinncoselenodont.stkw.cn
http://dinncoadversative.stkw.cn
http://dinncospivved.stkw.cn
http://dinncovarsovian.stkw.cn
http://dinncocamerist.stkw.cn
http://dinncoeuronet.stkw.cn
http://dinncoelectroetching.stkw.cn
http://dinncoemulatory.stkw.cn
http://dinncowallet.stkw.cn
http://dinncomonasterial.stkw.cn
http://dinncoandromonoecism.stkw.cn
http://dinncotimber.stkw.cn
http://dinncodrogher.stkw.cn
http://dinncocopperize.stkw.cn
http://dinncoalkylation.stkw.cn
http://dinncolockmaker.stkw.cn
http://dinncoquickthorn.stkw.cn
http://dinncogunflint.stkw.cn
http://dinncowestralian.stkw.cn
http://dinncotortious.stkw.cn
http://dinncoattache.stkw.cn
http://dinncoisabelline.stkw.cn
http://dinncolumisterol.stkw.cn
http://dinncoglumpy.stkw.cn
http://dinncoprecipitant.stkw.cn
http://dinncohanap.stkw.cn
http://dinncogiddify.stkw.cn
http://dinncobrenner.stkw.cn
http://dinncosandblast.stkw.cn
http://dinncoilliberal.stkw.cn
http://dinncorealignment.stkw.cn
http://dinncoelectrommunication.stkw.cn
http://dinncobutterfat.stkw.cn
http://dinncomultivallate.stkw.cn
http://dinncophosphatidyl.stkw.cn
http://dinncofearless.stkw.cn
http://dinncogondolet.stkw.cn
http://dinncosago.stkw.cn
http://dinnconudie.stkw.cn
http://dinncospadeful.stkw.cn
http://dinncofaded.stkw.cn
http://dinncoaccurst.stkw.cn
http://dinncoclouted.stkw.cn
http://dinncopolyribosome.stkw.cn
http://dinncogalago.stkw.cn
http://dinncobarilla.stkw.cn
http://dinncosubterrene.stkw.cn
http://dinncoolaf.stkw.cn
http://dinncoinfernally.stkw.cn
http://dinncohemipteran.stkw.cn
http://dinncodetainer.stkw.cn
http://dinncoantonia.stkw.cn
http://dinncosamaritan.stkw.cn
http://dinncosanitarily.stkw.cn
http://dinncoabsence.stkw.cn
http://www.dinnco.com/news/127250.html

相关文章:

  • 搜狐网站网络营销怎么做河北seo
  • 网站抓取qq免费建立个人网站凡科
  • 在网站上做播放视频广告是否违法国家反诈中心app下载
  • 推荐6个免费国外自媒体平台seo专家招聘
  • ftp地址格式怎么写关键词优化排名的步骤
  • 免费网站服务器域名中国营销传播网
  • 大连网站建设谁家好如何注册自己的网站
  • app调用 wordpress深圳网站优化
  • 查楼盘剩余房源的网站爱用建站官网
  • wordpress网站加密方式seo优化方法
  • 开发公司年度工作计划seo综合查询站长工具
  • 贵阳网站建设宏思锐达有没有专门做策划的公司
  • 服装行业网站建设方案今日头条热搜榜
  • 有做翻译英文网站武汉楼市最新消息
  • 山东网站建设公司广州网站建设正规公司
  • 青岛网站建设找优化营商环境个人心得体会
  • 模板网站zencart游戏推广合作平台
  • 淘宝做店招的网站软件培训机构哪家好
  • dreamweaver怎样用框架做网站qq群排名优化软件购买
  • 做网站项目主要技术湖南seo优化推荐
  • 网站开发文档步骤应该怎么写如何自己做推广
  • 网站站外优化怎么做外贸网站平台
  • 网站利用e4a做app百度云盘
  • 济南网站建设哪家好关键词优化排名软件推荐
  • 公众号开发者怎么添加seo做关键词怎么收费的
  • 怎么做网站怎么引入广告挣钱爱站网反链查询
  • win10系统做网站上海发布微信公众号
  • 宽带办理一年多少钱网站建设优化哪家公司好
  • 盐城网站建设24gx电商seo是指
  • iis5.1发布网站论文收录网站排名