Skip to content

Instantly share code, notes, and snippets.

@wizard1066
Created April 12, 2020 15:06
Show Gist options
  • Save wizard1066/874a7fa43eb2b83614ca86fdd99eec19 to your computer and use it in GitHub Desktop.
Save wizard1066/874a7fa43eb2b83614ca86fdd99eec19 to your computer and use it in GitHub Desktop.
ddwtp11
struct DoDomino: View {
@State var column:Int
@State var dragOffset = CGSize.zero
@State var accumulated = CGSize.zero
@State var rotateAngle:Double = 0
@State var highImage:String
@State var lowImage:String
@State var flipper:Double = 0
var body: some View {
Domino(highImage: $highImage, lowImage: $lowImage, flipper: $flipper, index: $column)
.border(Color.black)
.rotationEffect(.degrees(self.rotateAngle), anchor: .center)
.gesture(TapGesture(count: 2)
.onEnded({ (_) in
withAnimation {
if self.rotateAngle < 360 {
self.rotateAngle += 90
self.flipper -= 90
} else {
self.rotateAngle = 0
self.flipper = 0
}
}
}
)
).onReceive(flipDominoPublisher, perform: { ( tupple ) in
let (domino,direction) = tupple
if domino == self.$column.wrappedValue {
withAnimation {
if self.rotateAngle < 360 {
self.rotateAngle += direction
self.flipper -= direction
} else {
self.rotateAngle = 0
self.flipper = 0
}
}
}
})
}
}
struct Domino: View {
@Binding var highImage:String
@Binding var lowImage:String
@Binding var flipper: Double
@Binding var index:Int
var body: some View {
return RoundedRectangle(cornerRadius: 8)
.fill(Color.clear)
.frame(width: 48, height: 96, alignment: .center)
.background(
VStack {
Image(highImage)
.resizable()
.frame(width: 32, height: 32, alignment: .top)
.padding(4)
.rotationEffect(.degrees(flipper), anchor: .center)
.onTapGesture(count: 2) {
flipDominoPublisher.send((self.index, 180))
}
Divider()
.frame(width: 24, height: 2, alignment: .center)
Image(lowImage)
.resizable()
.frame(width: 32, height: 32, alignment: .bottom)
.padding(4)
.rotationEffect(.degrees(flipper), anchor: .center)
.onTapGesture(count: 2) {
flipDominoPublisher.send((self.index, -180))
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment