Giter Site home page Giter Site logo

rectutil's Introduction

RectUtil

RectUtil is a syntactic sugar of CGRect. It will make your life easy while interacting with CGRect.

I have accomplished it using 2 approaches:

  • Utility class
  • UIView Extension

Utility

Util is explained with example XCode project on the blog post RectUtil - iOS CGRect Utility Class .

Extension

A swifty approach of achieving this using UIView extension.

extension UIView {
    
    // MARK: Geometry Methods
    func centeredRectWithinRect(containerRect: CGRect)
    {
        self.frame.origin.y = containerRect.size.height / 2 - (self.frame.size.height / 2)
        self.frame.origin.x = containerRect.size.width / 2 - (self.frame.size.width / 2)
    }
    
    func distanceBetweenTwoPoints(point1: CGPoint, point2: CGPoint) -> CGFloat
    {
        let dx: CGFloat = point2.x - point1.x
        let dy: CGFloat = point2.y - point1.y
        
        return sqrt(dx*dx + dy*dy)
    }
    
    func setXOrigin(xOrigin: CGFloat)
    {
        self.frame.origin.x = xOrigin
    }
    
    func setYOrigin(yOrigin: CGFloat)
    {
        self.frame.origin.y = yOrigin
    }
    
    func setXAndYOrigin(xOrigin: CGFloat, yOrigin: CGFloat)
    {
        self.setXOrigin(xOrigin: xOrigin)
        self.setYOrigin(yOrigin: yOrigin)
    }
    
    func setWidth(width: CGFloat)
    {
        self.frame.size.width = width
    }
    
    func setHeight(height: CGFloat)
    {
        self.frame.size.height = height
    }
    
    func setSize(size: CGSize)
    {
        self.frame.size = size
    }
    
    func setXOriginAndWidth(xOrigin: CGFloat, width: CGFloat)
    {
        self.setXOrigin(xOrigin: xOrigin)
        self.setWidth(width: width)
    }
    
    func setYOriginAndHeight(yOrigin: CGFloat, height: CGFloat)
    {
        self.setYOrigin(yOrigin: yOrigin)
        self.setHeight(height: height)
    }
    
    func setBottom(bottom: CGFloat)
    {
        self.setHeight(height: bottom - self.frame.origin.y)
    }
    
    func shrinkHeight(shrinkOffset: CGFloat)
    {
        self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height - shrinkOffset)
    }
}

ExtensionPlayground.playground contains example for UIView extension for CGRect helpers.

Compatibility

Verified that this repository's code works in XCode 8 with Swift 3.0

Author ๐Ÿ™๐Ÿป

Web: Khawaja Farooq

Twitter: @khfarooq

Medium: @khfarooq

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.