• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Swift Introduction Core Graphics for Beginners Part 2

Home ios Swift Introduction Core Graphics for Beginners Part 2

Swift Introduction Core Graphics for Beginners Part 2

Feb 8, 2017 | Posted by Andrew | ios, swift, tutorial, xcode |

In our last post we looked at core graphics, what it is, why use it and how to draw a square and circle with it. In this post we are going to look at moving this drawing code to work with UIView. IF you haven’t already check out the last post and download the source code, or create a new single view application and you can follow the code below.

First of all in Swift create a new class, go File->New->Cocoa Touch Class. Then name it SquareShape and select UIView for Subclass of UIView.

Now we have added that, uncomment the draw code, and add in the following which will draw a square

    override func draw(_ rect: CGRect) {
        // Drawing code
        let squarePath = UIBezierPath() // 1
        squarePath.move(to: CGPoint(x: 0, y: 0)) // 2
        squarePath.addLine(to: CGPoint(x: 100, y: 0))
        squarePath.addLine(to: CGPoint(x: 100, y: 100))
        squarePath.addLine(to: CGPoint(x: 0, y: 100))
        
        squarePath.close() // 5
        
        UIColor.red.setFill()
        squarePath.fill()
    }

Head over to your Main.storyboard, and drag a UIView from the object explorer in the bottom right to your view. Now select the UIView and in the top right Identity Inspector change the class to the one we just made, SqureShape.

Run the app and you will see the square draw to the UIView we just added to the storyboard, neato!

This is all well and good, but it’s a bit annoying that it doesn’t render in the storyboard when are designing our app. What if there were a way to have it show up as a red square on the storyboard? Well luckly since XCode 6 there is a way for us to do this. Go back to the SquareShape.swift class, and above the Class SquareView: UIView add the following:

@IBDesignable

Go back to the Main.storyboard and after a few seconds it will render as follows:

So what else can we do with this drawing? Well instead of drawing it in a UIView we could draw it in our own custom UIButton! Lets draw a circle button. First of all in Swift create a new class, go File->New->Cocoa Touch Class. Then name it CircleShape and select UIButton for Subclass of.

Then code this class as follows to draw a circle

import UIKit

@IBDesignable
class CircleShape: UIButton {

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(ovalIn: rect)
        UIColor.magenta.setFill()
        path.fill()
    }

}

Now go back to your storyboard, drag a UIButton onto it and change the custom class to circle shape.

Change the button text to Run, and you will have a circle button!

Lets connect up our custom button to run some code. Select View Controller in the storyboard, then select assistant editor in the top right. Now select our button, and hold the control key and drag the line into our View Controller code. Create an action named
“doWork”.

Add in the following code to make an alert show to the user.

    @IBAction func doWork(_ sender: Any) {
        let alertController = UIAlertController(title: "In Progress", message: "Coding app", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alertController, animated: true, completion: nil)
    }

Now we have our own custom button that was drawn with core graphics running code, pretty cool.

download source code

 

Tags: circlecore graphicssquareswift 3uibuttonuiview
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift Introduction Core Graphics for Beginners

Feb 6, 2017

Core graphics is a lightweight framework used to make 2d[...]

Swift Animated Square to Circle Transform with Core Graphics

Feb 5, 2017

In this tutorial we will be making a Square button[...]

Swift – Make a Trivia Quiz Game

Feb 16, 2017

In this Swift quiz app tutorial we will look at[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next