[source code] iOS Development with Swift Tutorial - 23 & 24 - Drawing Shapes

+2 Bucky Roberts · February 5, 2015
DrawExamples.swift
import UIKit

class DrawExamples: UIView {

    override func drawRect(rect: CGRect) {
        //context is the object used for drawing
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 3.0)
        CGContextSetStrokeColorWithColor(context, UIColor.purpleColor().CGColor)
        
        /* Straight line
        CGContextMoveToPoint(context, 30, 30)
        CGContextAddLineToPoint(context, 150, 320)
        */
        
        /* Ugly custom shape
        CGContextMoveToPoint(context, 50, 50)
        CGContextAddLineToPoint(context, 90, 130)
        CGContextAddLineToPoint(context, 180, 100)
        CGContextAddLineToPoint(context, 90, 60)
        CGContextAddLineToPoint(context, 50, 50)
        */
        
        //Rectangle
        let rectangle = CGRectMake(50, 50, 200, 400)
        CGContextAddRect(context, rectangle)
        
        //Actually draw the path
        CGContextStrokePath(context)
    }

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Amritpal singh · April 23, 2017
 first i wrote whole cade. then i copied and paste but every thing is messed up. see in photo
  • 1

iOS Development / Swift

129,737 followers
About

This forum is for questions, comments, and general discussion about iOS Development and Swift.

Links
Moderators
Bucky Roberts Administrator