r/iOSProgramming • Swift • Jan 07 '21

Solved! Took me 2 days to implement the capture button almost the same with stock iOS camera. Along with its animation. I’m so proud of myself!

Post image
166 Upvotes

34 comments sorted by

10

u/danen_ Jan 07 '21

Do you work with some custom UIView or with UIButton with layers? Nice work btw

8

u/dadofbimbim Swift Jan 07 '21

Thanks! The inner circle is a UIButton and the border is a UIView that is transparent. You get the idea.

4

u/javaHoosier Jan 07 '21

Nice job! Now try it with SwiftUI in 30 mins and you’ll see how awesome it is haha.

1

u/dadofbimbim Swift Jan 07 '21

Haha! I haven’t yet finished the SwiftUI course and implementing it in SwiftUI sounds scary.

4

u/javaHoosier Jan 07 '21

That’s funny, because I feel the opposite haha. For a fun exercise I’ll implement it in SwiftUI and then you can compare the code for comparison. SwiftUI is just young and UIKit is still important and relevant.

1

u/dadofbimbim Swift Jan 07 '21

Yo that is so cool! Can you share it to me after?

3

u/javaHoosier Jan 07 '21 edited Jan 07 '21

Heres the code!

There are several ways to do this and which is right could be up for debate and preference. You could just make some shapes and then make them tappable like in the Naive Button. You could make a button and have the Circles in the label parameter (I didn't make this example though). I just put the same logic into button styles. Instead of on the button configuration it would just go in the view of the button.

Button styles have an isPressed parameter but unfortunately it only activates when actually touching. So I had to create a State variable to store the size and corner radius if recording or not.

The animations are really simple and you can change it by passing in an Animation like this but the default is linear and the speed worked well enough:

WithAnimation(Animation...) {
    isRecording.toggle()
}

So if you copy and paste this and name the project RecordButtons it will work. I only gave it the Color and HStack so you could see the buttons lined up next to each other with a gray background. It's crazy that you can make the recording button with 20 lines of code right? Haha

2

u/dadofbimbim Swift Jan 08 '21

Nice! Let me dig in

2

u/PrettyMuchIt530 Jan 08 '21

Is the animation just changing the size and corner radius? Cause if you just have a square button, you can make it circular with a corner radius that’s half the height, then in the uiview.animate just downscale it and make the radius much smaller

1

u/dadofbimbim Swift Jan 08 '21

Pretty much that’s what I did. Plus the Autolayout constraints and border width and padding calculation.

4

u/NotlivingNordead Jan 07 '21

I tried clicking the button on the photo to see what would happen. Maybe I am retarded?

2

u/juanjovn Jan 07 '21

But I mentioned time constraint because this is for a client and I am working hourly.

you are not alone haha

3

u/larsonthekidrs Swift Jan 07 '21

Is there a GitHub link where I can check out how you achieved this?

2

u/dadofbimbim Swift Jan 07 '21

I’ll write about this in a couple of days.

1

u/larsonthekidrs Swift Jan 07 '21

Thank you!

2

u/dadofbimbim Swift Jan 08 '21

Hi! Here’s a quick write up. Let me know if you tried it already.

https://lwgmnz.me/2021/01/08/create-a-camera-shutter-in-swift/

1

u/larsonthekidrs Swift Jan 08 '21

I’ll try this as soon as I get home

4

u/mRs- Jan 07 '21

How many hours did you spent in Interfaxe builder for this?

6

u/dadofbimbim Swift Jan 07 '21

Hmm an hour or two. The animation was the thing that made it complicated

2

u/juanjovn Jan 07 '21

How does the animation look at the end? Can you post a gif or something?

2

u/dadofbimbim Swift Jan 07 '21

I don’t know how to upload gif here so I tweeted it.

https://twitter.com/dirtylowprofile/status/1347272960231395328?s=21

1

u/juanjovn Jan 07 '21

Neat. Looks so nice!

1

u/dadofbimbim Swift Jan 07 '21

Here’s the animation: https://twitter.com/dirtylowprofile/status/1347272960231395328

It’s not entirely same as stock camera but at least I got the circle to square transition. I’ll write a post about it in a couple of days.

1

u/bcgroom Jan 07 '21

I have done the exact same thing at work. Did you do it because you didn’t want the confirmation screen that comes after this when you use UIImagePickerController?

1

u/dadofbimbim Swift Jan 08 '21

It’s because our app has a different logic and design from any default camera that records video.

1

u/aheze Swift Jan 08 '21

Nice work! I made something very similar recently too. I like your animation better though.

2

u/dadofbimbim Swift Jan 08 '21

Dope! I think we have the same animation but only differs in duration.

1

u/YungAchillies Jan 08 '21

Good job

1

u/dadofbimbim Swift Jan 08 '21

Thanks! 🤗

0

u/[deleted] Jan 07 '21

[deleted]

8

u/dadofbimbim Swift Jan 07 '21

Thanks! Yes I learned a lot and this is why I love programming and mobile development in general. But I mentioned time constraint because this is for a client and I am working hourly.

4

u/thrgd Jan 07 '21

If you get payed for that it is even better! Congrats! 🙂

0

u/[deleted] Jan 07 '21

[deleted]

2

u/dadofbimbim Swift Jan 07 '21

Sure! Next week or in a couple of days.

2

u/dadofbimbim Swift Jan 08 '21

Hi! Here’s the quick write up on how I developed it. Let me know if you have any questions.

https://lwgmnz.me/2021/01/08/create-a-camera-shutter-in-swift/