当前位置:网站首页>SwiftUI 4 新功能之实时获取点击位置 .onTapGesture { location in} (教程含源码)
SwiftUI 4 新功能之实时获取点击位置 .onTapGesture { location in} (教程含源码)
2022-07-26 19:27:00 【知识大胖】
SwiftUI 4 添加了对跟踪点击位置的支持,它是一种 CGPoint 类型,可以为我们提供点击的确切位置。默认情况下,提供的位置来自视图的局部坐标系。

让我们创建一个示例,我们将创建一个橙色圆圈,该圆圈将动画到点击位置。
struct TapGestureLocationExample: View {
@State private var pos = CGPoint(x: 100, y: 100)
var body: some View {
VStack {
Circle()
.fill(Color.orange.gradient)
.frame(width: 50)
.position(pos)
Spacer()
}
.background(Color.gray.opacity(0.3))
.onTapGesture { location in
withAnimation(Animation.easeOut(duration: 2)) {
pos = location
}
}
}
}
我们可以使用以下语句指定坐标系:
.onTapGesture(coordinateSpace: .global)
让我们更新我们的代码:
struct TapGestureLocationExample: View {
@State 边栏推荐
- C# 使用默认转型方法
- eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
- A little cool, explore space with.Net Maui
- Zabbix调用api检索方法
- LeetCode_回溯_中等_216.组合总和 III
- Cookie和Session
- EtherCAT 同步模式
- JWT 实现登录认证 + Token 自动续期方案,这才是正确的使用姿势!
- Decompile jar files (idea environment)
- BluePrism流程业务对象的组件功能介绍-RPA第三章
猜你喜欢

一维数组定义与使用

Meeting seating & submission of meeting OA
[cache series] advantages, disadvantages and choices of several cache read-write schemes

N圆最密堆积、最小外接正方形的matlab求解(二维、三维等圆Packing 问题)

20220725树状数组入门反思

Parallel execution (II). Multiprocessing

DOM case: 10 second countdown - write jump page related knowledge

Record an analysis of a.Net property management background service stuck

解决AttributeError: module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu

A little cool, explore space with.Net Maui
随机推荐
cv2.resize()
【JVM 系列】JVM 调优
Dio problem summary
SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
this指向-超经典面试题
Is flush reliable? I just started to learn financial management. Is it safe to open a securities account?
DevSecOps,让速度和安全兼顾
Excel-VBA 快速上手(十一、字符串常用操作)
3万脱发人,撑起一个IPO
移动端video兼容你需要知道的几点
These 22 drawing (visualization) methods are very important and worth collecting!
数组操作增,删,改,查
EtherCAT 同步模式
【机器学习】变量间的相关性分析
2022年下半年(软考高级)信息系统项目管理师报名条件
【MySQL】 - 索引原理与使用
UE5编辑器Slate快速入门【开篇】
Kingbases SQL language reference manual of Jincang database (15. SQL statement: create materialized view to create schema)
Kingbases SQL language reference manual of Jincang database (16. SQL statement: create sequence to delete)
A little cool, explore space with.Net Maui