我有三(3)SF Symbols个分组,并安排在一个特定的关系彼此.现在我想让整个小组作为一个项目,围绕一个特定点进行旋转.

enter image description here

我花了几个小时的时间进行试错(主要是错误),试图调整paddingoffsetsanchor points的组合.

上下文:

这个可旋转的视图将被用作iOS 17 Annotation的一部分,使用MapKit来标记指示行进方向的特定GPS位置(因此旋转比特和所需的锚点).

有一个更好的方法来实现我的最终目标1.我对所有的 idea 都持开放态度. :-)

以下是我的代码:

import SwiftUI

struct StarArrowView: View {
   
   var bumpColor: Color
   @State var rotaAtAnchor = false
   var body: some View {
      ZStack {
         Divider()
         Divider().offset(y: 25)
         Divider().offset(y: -25)
         Divider().rotationEffect(.degrees(90))
         Divider().rotationEffect(.degrees(90)).offset(x: 25)
         Divider().rotationEffect(.degrees(90)).offset(x: -25)
         Group {
            HStack(spacing: 0) {
               Image(systemName: "star.fill")
                  .symbolRenderingMode(.palette)
                  .foregroundStyle(bumpColor)
                  .font(.system(size: 30, weight: .light))
                  .padding(0)
                  .offset(x: 8, y: 6)
               Image(systemName: "line.diagonal.arrow")
                  .foregroundStyle(bumpColor)
                  .font(.system(size: 40, weight: .light))
                  .padding(0)
                  .offset(x: -10, y: -15)
               Text("➘")
                  .symbolRenderingMode(.palette)
                  .foregroundStyle(bumpColor, bumpColor)
                  .font(.system(size: 30, weight: .ultraLight))
                  .rotationEffect(.degrees(190), anchor: .topLeading)
                  .padding(0)
                  .offset(x: 0, y: 0)
            }
            .padding(.vertical, -20)
            .padding(.horizontal, -5)
            .rotationEffect(.degrees(45))
            .offset(x: 15, y: 22)
            
            
            .rotationEffect(.degrees(rotaAtAnchor ? 0 : 180), anchor: .trailing) //Anchor Position
            .animation(Animation.spring ().repeatForever(autoreverses: true))
            .onAppear() {
               self.rotaAtAnchor.toggle()
            }
         }
      }
   }
}
   


#Preview {
   StarArrowView(bumpColor: .heatmap10)
}

推荐答案

这个问题的棘手之处在于找准了锚点.一种方法是使用带有小数位置的UnitPoint.或者,为了使用组的一条边作为锚点,需要将指向上方的箭头移出组的边界.这可以使用覆盖来完成.

以下是让它发挥作用的一种try .以下是一些注意事项:

  • 当您正在构建组并查找锚点时,在各个项目后面放置一个彩色背景以使框架变得可见是有帮助的.
  • ZStack是用来组合符号的.我建议,这比HStack的效果更好,因为它涉及到一些重叠.
  • 在可能的情况下,填充优先于偏移使用.这样,项目将显示在其在布局中的真实位置.
  • 指向上的箭头作为覆盖应用于对齐为.topTrailing的组.然后,使用x偏移将其移动到组的边缘上.
  • 为了确保指向上方的箭头正好位于后缘的一半上方,应为其指定固定的宽度(任何大于自然宽度的大小都可以),然后x偏移量为该宽度的一半.
ZStack {
    Divider()
    Divider().offset(y: 25)
    Divider().offset(y: -25)
    Divider().rotationEffect(.degrees(90))
    Divider().rotationEffect(.degrees(90)).offset(x: 25)
    Divider().rotationEffect(.degrees(90)).offset(x: -25)

    ZStack(alignment: .leading) {
        Image(systemName: "star.fill")
            .symbolRenderingMode(.palette)
            .foregroundStyle(bumpColor)
            .font(.system(size: 30, weight: .light))
            .rotationEffect(.degrees(45))
        Image(systemName: "line.diagonal.arrow")
            .foregroundStyle(bumpColor)
            .font(.system(size: 40, weight: .light))
            .rotationEffect(.degrees(45))
            .padding(.leading, 30)
    }
    .padding(.trailing, 10)
    .overlay(alignment: .topTrailing) {
        Text("➘")
            .symbolRenderingMode(.palette)
            .foregroundStyle(bumpColor, bumpColor)
            .font(.system(size: 30, weight: .ultraLight))
            .rotationEffect(.degrees(235))
            .frame(width: 30)
            .offset(x: 15)
    }
    .rotationEffect(.degrees(rotaAtAnchor ? 180 : 0), anchor: .trailing)

    // Fine adjustment of the position of the entire group
    .padding(.leading, 30)
    .padding(.top, 20)

    .animation(Animation.spring ().repeatForever(autoreverses: true), value: rotaAtAnchor)
    .onAppear() {
       self.rotaAtAnchor.toggle()
    }
}

Animation

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

彩色进度线--SWIFT

如何创建自定义组件来接受(和传递)所有可能的Textfield参数?

在iOS 17脉冲符号效果不起作用的情况下制作UIBarButtonItem动画(没有UIImageView)

当包含UITextView的inputAccessoryView显示键盘时,iOS UITableView内容会出现UINavigationBar奇怪的错误

SWIFT用户界面检测手势的开始和结束

如果使用 .onAppear 设置,为什么 SwiftUI Picker 不显示正确的选定值

Toast 不显示 Flutter

iOS - 判断开发者模式是否开启 (Swift)

Xcode14 启动时崩溃:dyld:库未加载:/usr/lib/swift/libswiftCoreGraphics.dylib

react 在 android 上运行的本机应用程序,但在 iOS 上出现问题,详细信息在描述中共享

如何在 SwiftUI 中只显示一次随机数组的项目?

无法为 Flutter 项目构建 ipa [CocoaPods 找不到 pod "GoogleAppMeasurement" 的兼容版本:]

SwiftUI - 在ForEach的每个元素之间自动添加分隔符

在 Swift 中按下返回键时在文本字段之间切换

如何在 UIActivityViewController 中设置邮件主题?

如何更新推送通知服务证书

'无效更新:第 0 节中的无效行数

闭包不能隐式捕获变异的 self 参数

Flutter:如何创建一个新项目