5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SwiftUI】複数ジェスチャーの実装

Posted at

#SimultaneousGesture
SwiftUIで複数のジェスチャーを実装する方法がわからなかったので、備忘録として残しておきます。
複数ジェスチャーの実装をするにはSimultaneousGestureを実装する必要があります。
以下はオブジェクトの拡大・縮小と移動の2つのジェスチャーを設定したコードになります。
SimultaneousGestureの引数に設定したいGestureを設定することで、複数ジェスチャーの実装ができます。

let magnificationGesture = MagnificationGesture()
   .onChanged { scale = $0 * initialScale }
   .onEnded{ _ in initialScale = scale }
let dragGesture = DragGesture()
   .onChanged { offset = CGSize(width: initialOffset.width + $0.translation.width, height: initialOffset.height + $0.translation.height) }
   .onEnded{ _ in initialOffset = offset }

RoundRectangle()
   .gesture(SimultaneousGesture(magnificationGesture, dragGesture))

#参考
https://software.small-desk.com/development/2021/06/28/swiftui-pinch-and-drag/

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?