Detect single/multi-finger gesture Swiftui - swiftui

How to detect number of finger interaction with Gesture in SwiftUI Gesture. With UIKit we can find number of find it from
let numberOfTouches = panGestureRecognizer.numberOfTouches
Any thought?

I am able to find my solution using UIKit UIView. Actually made container view(UIVIewRepresentable) which implements the PanGesture. I have used this view to to hold SwiftUI view. I can easily find and able to handle Panning behaviour.

Related

How to implement vertical scroll page on watchOS with SwiftUI

I want to implement a vertical scroll page like system workout app.
Now I can get a similar effect with this:
    TabView {
      page1
        .rotationEffect(.degrees(90))
      page2
        .rotationEffect(.degrees(90))
    }.tabViewStyle(.page(indexDisplayMode: .automatic))
    .rotationEffect(.degrees(-90))
But not good as system workout app does. It supports digital crown and dynamic show indicator and custom color.
So , how to implement that vertical scroll effect?
https://github.com/fredyshox/PageView
This repo works well except crown supports.

ZIPFoundation progress displayed using SwiftUI ProgressView

I'm having an application in SwiftUI and I am using ZIPFoundation to uncompress an archive. This works fine. At the moment I am using native SwiftUI ProgressView(title) to display during the uncompress process, again working fine, anyway it only displays a rotating wheel and no progress scale.
I would like to change to ProgressView(title, value, total) to display the scale, anyway not sure how. I have found this example, but it uses UIKit and addObserver() and I am not sure how to adapt this to SwiftUI, or if there is better way with SwiftUI.
Can anyone please help.

ScrollViewReader scrollTo with .center anchor bug?

So I'm try to use ScrollViewReader to programmatically scroll a horizontal scroll view. I thought it would work like scrollToItem with .centeredHorizontally in UIKit, and for the most part it does, but the last few elements in the scroll view are being forcefully scrolled to the center of the screen, despite the fact that the scroll view isn't normally able to scroll that far over (without snapping back after releasing the drag, at least). This ends up creating white space across the trailing half of the screen.
I've seen some other questions about this and it seems like the general opinion is that it's not a bug? On the one hand I suppose we're telling the scroll view to center the item, and it's doing just that -- so, not a bug? On the other hand, that's not how similar functionality worked in UIKit. Also, this behavior is only happening on the trailing end of the scroll view! If it was the intended behavior I would expect that scrolling to the first element in the scroll view using .center anchor would force it into the center of the screen and leave leading white space, but this doesn't happen.
Is there an elegant solution to this? Or do we have to calculate the width of our elements + spacing and figure out based on the screen width whether we should anchor .center or just scroll to the last element with anchor .trailing in order to replicate the UIKit behavior?
I found a package (Amzd/ScrollViewProxy) that was made before ScrollViewReader was released that functions much the same as ScrollViewReader, but also seems to not have the bug (if it is a bug) detailed in the question.
Usage examples can be seen on the repository page, but here's a quick minimal example.
ScrollView(.horizontal) { scrollProxy in
ForEach(sections) { section in
Text(section.text)
.scrollId(section.id)
}
.onChange(of: index) {
scrollProxy.scrollTo(
sections[index].id,
alignment: .center
)
}
}
The package adds a convenience init to ScrollView to give access to the scrollProxy.
I can confirm this behavior and think it should be considered a bug. Especially since the scroll view will "jump" into position on the first touch event.
As of iOS 15.4 Beta 1 this is fixed for me. Maybe give it another try.

Scrolling region in cocos2d version 2

I am trying to implement a help screen in my cocos2d game, using cocos2d version 2.0. My screen will have a title bar ("Help") at the top and then the rest of the screen below that is where I want to put a scrolling help section. Ideally I would be able to put both text and images into this help window.
The problem is that cocos2d does not have any functionality like UIScrollView, and from what I have seen doing Google searches, every custom solution I have found seems to have problems with various bugs popping up on various devices.
I have tried these solutions thus far:
CCScrollLayer: http://www.cocos2d-iphone.org/forum/topic/17118/page/3
Scrolling CCNode: http://tonyngo.net/2011/11/scrolling-ccnode-in-cocos2d/
CCScrollView: http://bitbattalion.com/2011/09/uikit-uiscrollview-and-cocos2d/
The closest thing I got to work was embedding a UITextView but that seemed to randomly crash after a few scrolls so it seems unreliable to me.
Does anyone know of a good simple robust solution to this problem? It seems like it should be straightforward but it isn't.
I recommend that you make new class say:(HelpViewClass) and implement it with an UIScrollView and add whatever you want to add on UIScrollView and then you can use this as a child to your layer.
Steps
Make a class - inherited with UIView
Add UIScrollView to the View.
Add Your components to it.
Add this UIView to the HelpLayer.
You can add any UIKit component to the cocos2d Layer by using this
[[[CCDirector sharedDirector] view] addSubView:scrollView];
Note : Remove all UI component when you go back from this HelpLayer.
I think this may help you !

Integrating MapKit with Cocos2D

I'm a bit new with MapKit and have been trying to find the answer to this question:
Can I integrate, or to what extent can I integrate MapKit with Cocos2d?
In having this question answered, I hope that somebody could point me in the right direction on how to approach this scenario:
I am developing a Cocos2D app in which one part of the app would be the MapKit application, showing location and annotations (basically everything that comes with MapKit), and the user has the option to switch to the Cocos2D part. (Switching back and forth between the Map and the Game).
If the two were integrated, would there then be a way to overlay a Cocos2D Menu system over the MapKit and treat MapKit as simply a CCLayer or CCNode? or do I need to treat these two as separate entities altogether by only using UIKit buttons and menus with MapKit, and the only real mix between them is the passing of data from MapKit on to Cocos2D view on transition and vice versa?
Would it follow the same concept as integrating UIKit and Cocos2D in this answered question or this tutorial? Or do I have to use a special wrapper class such as CCUIViewWrapper? I read that CCUIViewWrapper is always drawn on top, so it could possibly not allow me to draw a Cocos2D Menu on top of the map, or even overlay a Cocos2D object once a UIKit button is pressed in the MapKit App.
Any help would be appreciated...Thanks!
I think using CCUIViewWrapper will be a good idea, about the question that CCUIViewWrapper always draw on the topmost layer of View I think that is kinda wrong(I am not sure) as CCUIViewWrapper returns the object in the format of CCNode so you can add it anywhere on the screen you would like to (If I was in your place I would have first tested this with adding a small sprite on top of a MapKit View in a cocos2d project just to be sure).
-Ankur