Guest User

Untitled

a guest
Mar 6th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.53 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  SquareToCircle
  4. //
  5. //  Created by Andrew Seeley on 5/2/17.
  6. //  Copyright © 2017 Seemu. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.    
  13.    
  14.     let squareLayer = CAShapeLayer()
  15.    
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.         // Do any additional setup after loading the view, typically from a nib.
  19.        
  20.      
  21.         // Add the square to our view
  22.         let squareCenter = CGPoint(x: 200, y: 200)
  23.         let square = squarePathWithCenter(center: squareCenter, side: 100)
  24.         squareLayer.path = square.cgPath
  25.         squareLayer.fillColor = UIColor.red.cgColor
  26.         self.view.layer.addSublayer(squareLayer)
  27.        
  28.        
  29.     }
  30.    
  31.     func squarePathWithCenter(center: CGPoint, side: CGFloat) -> UIBezierPath {
  32.         let squarePath = UIBezierPath()
  33.         let startX = center.x - side / 2
  34.         let startY = center.y - side / 2
  35.         squarePath.move(to: CGPoint(x: startX, y: startY))
  36.         squarePath.addLine(to: squarePath.currentPoint)
  37.         squarePath.addLine(to: CGPoint(x: startX + side, y: startY))
  38.         squarePath.addLine(to: squarePath.currentPoint)
  39.         squarePath.addLine(to: CGPoint(x: startX + side, y: startY + side))
  40.         squarePath.addLine(to: squarePath.currentPoint)
  41.         squarePath.addLine(to: CGPoint(x: startX, y: startY + side))
  42.         squarePath.addLine(to: squarePath.currentPoint)
  43.         squarePath.close()
  44.         return squarePath
  45.     }
  46.    
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment