Revealing strokes in Swift

Good day,

I'm new to Swift (and object oriented languages ​​in general) and I'm building an iPad app that allows you to draw with your finger. I am currently trying to properly manage multitouch events and I would like to track strokes. The idea is to draw only one touch and discard the rest, ignoring them even after the starting stroke has finished.

For this, I need to identify each touch, but the UITouch object does not contain an ID. In fact, since the strokes are stored in the NSSet, they are not ordered.

Erring on the internet, I found a possible solution storing touch addresses in CFDictionary. This seems reasonable in Objective-C, but Swift is not very pointer-friendly and I was unable to create a CFDictionary with UITouch pointers as a value.

Does anyone know how to handle this? Am I using CFDictionaries incorrectly (I was trying to have UnsafePointer<UITouch>

as a value)? Is there any other solution I missed?

Thank you for your help.

+3


source to share


1 answer


I only need a simple touch screen key and used the UITouch interpolation string to get it. My touch string interpolation looked like this:

<UITouch: 0x1046355b0> phase: Began tap count: 1 force: 0.000 window: <UIWindow: 0x10462beb0; frame = (0 0; 414 736); gestureRecognizers = <NSArray: 0x1c0247fe0>; layer = <UIWindowLayer: 0x1c02325a0>> view: <SCNView: 0x104624be0 | scene=<SCNScene: 0x1c4129ec0> sceneTime=0.000000 frame={{0, 0}, {414, 736}} pointOfView=<SCNNode: 0x1c43f9400 rot(0.000000 1.000000 0.000000 3.141593) | camera=<SCNCamera: 0x10475af60> | no child>> location in window: {220, 344} previous location in window: {220, 344} location in view: {220, 344} previous location in view: {220, 344}

      

I needed the second part, so I wrote the following method to extract it.

func getTouchKey(touch:UITouch) -> String {
    let interpolation = "\(touch)"
    let parts = interpolation.split(separator: " ", maxSplits: 3, omittingEmptySubsequences: true)
    let touchKey = String(parts[1])
    return touchKey
}

      



In this case, it returns:

0x1046355b0>

      

I could clear it if needed, but since it already repeats for the entire touch time, I didn't add anything else.

0


source







All Articles