SwiftUI 自定义组件之 音量滑块和苹果音乐滑块动画特效(教程含源码)_ios 滑块动画.docx

上传人:暗伤 文档编号:97192498 上传时间:2024-04-29 格式:DOCX 页数:8 大小:145.72KB
返回 下载 相关 举报
SwiftUI 自定义组件之 音量滑块和苹果音乐滑块动画特效(教程含源码)_ios 滑块动画.docx_第1页
第1页 / 共8页
SwiftUI 自定义组件之 音量滑块和苹果音乐滑块动画特效(教程含源码)_ios 滑块动画.docx_第2页
第2页 / 共8页
点击查看更多>>
资源描述

《SwiftUI 自定义组件之 音量滑块和苹果音乐滑块动画特效(教程含源码)_ios 滑块动画.docx》由会员分享,可在线阅读,更多相关《SwiftUI 自定义组件之 音量滑块和苹果音乐滑块动画特效(教程含源码)_ios 滑块动画.docx(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、SwiftUI 自定义组件之 音量滑块和苹果音乐滑块动画特效(教程含源码) 实战需求SwiftUI自定义组件之 音量滑块和苹果音乐滑块动画特效本文价值与收获看完本文后,您将能够作出下面的界面1:24-I实战代码1、主界面1import SwiftUI23struct ContentView: View 45 State private var playerDuration: TimeInterval = 1006 private let maxDuration = TimeInterval(240)78 State private var volume: Double = 0.39 priva

2、te var maxVolume: Double = 11011 State private var sliderValue: Double = 1012 private var maxSliderValue: Double = 1001314State private var color: Color = .white1516 var body: some View 17 ZStack 18 LinearGradient(colors: .brown, .brown.opacity(0.1), startPoint: .top, endPoint: .bottom)19 .ignoresSa

3、feArea()20 .overlay(.ultraThinMaterial)2122VStack(alignment: .leading, spacing: 60) 23individualComponentsSection2425Spacer()2627.padding(.horizontal, .top, 50)28293031private var individualComponentsSection: some View 32VStack(alignment: .trailing, spacing: 20) 33VerticalVolumeSlider(value: $volume

4、, inRange: 0.maxVolume, activeFillColor: color, fillColor: normalFillColor, emptyC343536.frame(height: 130)3738CustomSlider(value: $sliderValue, inRange: 0.maxSliderValue, activeFillColor: color, fillColor: normalFillColor, empt394041.frame(height: 40)4243MusicProgressSlider(value: $playerDuration,

5、inRange: TimeInterval.zero.maxDuration, activeFillColor: color, fillColor444546.frame(height: 40)4748VolumeSlider(value: $volume, inRange: 0.maxVolume, activeFillColor: color, fillColor: normalFillColor, emptyColor: em495051.frame(height: 30)5253 /ColorPicker(selection: $color) 54/Text(Select Color)

6、55/56575859private var normalFillColor: Color color.opacity(0.5)60private var emptyColor: Color color.opacity(0.3) 616263struct ContentView_Previews: PreviewProvider 64static var previews: some View 65ContentView()66672、自定义Slider12 import SwiftUI34 struct CustomSlider: View 5 Binding var value: T6 l

7、et inRange: ClosedRange7 let activeFillColor: Color8 let fillColor: Color9 let emptyColor: Color10 let height: CGFloat11 let onEditingChanged: (Bool) - Void1213 / private variables14 State private var localRealProgress: T = 015 State private var localTempProgress: T = 016 GestureState private var is

8、Active: Bool = false1718 var body: some View 19 GeometryReader bounds in20 ZStack 21 VStack 22 ZStack(alignment: .center) 23 Capsule()24 .fill(emptyColor)25 Capsule()26 .fill(isActive ? activeFillColor : fillColor)27 .mask(28 HStack 29 Rectangle()30 .frame(width: max(bounds.size.width * CGFloat(loca

9、lRealProgress + localTempProgress), 0),31 Spacer(minLength: 0)3233)3435.frame(width: isActive ? bounds.size.width * 1.04 : bounds.size.width, alignment: .center)3637 /.shadow(color: .black.opacity(0.1), radius: isActive ? 20 : 0, x: 0, y: 0)38 .animation(animation, value: isActive)3940 .frame(width:

10、 bounds.size.width, height: bounds.size.height, alignment: .center)41 .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local)42 .updating($isActive) value, state, transaction in43 state = true4445 .onChanged gesture in46 localTempProgress = T(gesture.translation.width / bounds.size.width)4

11、7 value = max(min(getPrgValue(), inRange.upperBound), inRange.lowerBound)48 .onEnded value in49 localRealProgress = max(min(localRealProgress + localTempProgress, 1), 0)50 localTempProgress = 051)52 .onChange(of: isActive) newValue in53 value = max(min(getPrgValue(), inRange.upperBound), inRange.low

12、erBound)54 onEditingChanged(newValue)5556.onAppear 57localRealProgress = getPrgPercentage(value)5859.onChange(of: value) newValue in60if !isActive 61localRealProgress = getPrgPercentage(newValue)62636465.frame(height: isActive ? height * 2 : height, alignment: .center)666768private var animation: An

13、imation 69if isActive 70return .spring()71 else 72return .spring(response: 0.5, dampingFraction: 0.5, blendDuration:0.6)73747576private func getPrgPercentage(_ value: T) - T 77let range = inRange.upperBound - inRange.lowerBound78let correctedStartValue = value - inRange.lowerBound79let percentage = correctedStartValue / range80return percentage818283848586 private func getPrgValue() - T return (localRealProgress + localTempProgress) * (inRange.upperBound - inRange.lowerBound) + inRange.lowerBound项目文件1 578Custom-Slider-Control-main.zip2 链接: 密码:50pt

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁