TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Scratch Card app

Forums > Swift

Hi,

I'm learning Swift by following the great tutorials on this site, and I'm trying to write a simple Scratch Card game. I've figured out how to get two UIImageViews. You can pick a photo or take a picture to use in the background image view that needs to be revealed. The foreground image view loads a grey image, that then needs to be scratched away. I'm using a UIGraphicsImageRenderer to draw lines on the on grey image with a clear blendmode to cut through to see the image in the background image view.

The problem I'm having is tracking my finger to draw the lines, so I'm hoping you gues can help please. I'm using a Pan Gensture Recogniser and storing the location point in an array of CGPoints:

let location = gesture.location(in: view)
points.append(CGPoint(x: location.x, y: location.y))

My function that draws the picture and lines into the foregound image view is:

func drawLine(points: [CGPoint]) {

      let image = foreGroundRenderer.image { ctx in
          let fg = UIImage(named: "foreground")
          fg?.draw(at: CGPoint(x: 0, y: 0))

          if (points.count > 0) {
              let firstPoint = points.first
              ctx.cgContext.move(to: firstPoint!)
              for pointIndex in 1..<points.count {
                  ctx.cgContext.addLine(to: points[pointIndex])
              }
              ctx.cgContext.setLineCap(.round)
              ctx.cgContext.setLineWidth(50)
              ctx.cgContext.setBlendMode(.clear)
              ctx.cgContext.strokePath()
          }

      }

      foreGround.image = image
  }

I have two issues though.

1) The locations I store in my points array, track my finger but seem to be offset in both x and y. Is that a scaling issue with the size of the UIGraphicsImageRenderer? 2) What's the best place to call the drawLine function? I'm currently calling it in the Pan Gesture Recogniser IBAction, right after the line above where I append a new location to the array. While this seems to work, it very unresponsive and draws the lines quite slowly. I have a feeling there's a better place to call the drawLine function from but I'm not sure where.

Sorry my post is a bit long.

Mark

2      

Hi! My guess is that drawing the foreground image with each call to drawPoints is responsible for much of the slowdown.

Also if I remember correctly Core Graphics uses inverted coordinates compared to UIKit so that is probably why you are seeing the offset.

2      

Hi. Thanks for the reply.

Yes that's what I was thinking with regards to running the drawLine function. In other frameworks/languages there's usually some fuction you can override that runs every frame and I'd place it there. But I'm not sure UIKit has that. Something like viewDidLoad? Or some kind of event thing?

Ah yes. I'd forgotten that coordinates system is different. I'll see if I can fix that then.

2      

After a few days of googling and experimentation, I've found something that works but I think I've gone old school. I've replaced the Pan Gensture Recogniser, with override func touchesBegan, and override func touchesMoved. I fire the drawLine function from touchedMoved and this solved the lag issue. Then in the drawLine function, UIGraphicsImageRenderer, has been replaced by UIGraphicsBeginImageContext. That solved the finger tracking offset issue. I get the impression that what I'm using now is pretty old tech, but it seems to work much better.

2      

@thompson1970 Do you have a github for this? Im new to iOS programming and want to practice doing a similar program

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.