Sunday, 26 November 2017

Animate



1. Rotate view 180 degree

Code -  let anticlockAnimation = CABasicAnimation(keyPath: "transform.rotation")
        anticlockAnimation.fromValue = CGFloat.pi
        anticlockAnimation.toValue = 0
        anticlockAnimation.isAdditive = true
        anticlockAnimation.duration = 3.0
        self.imageview.layer.add(anticlockAnimation, forKey: "rotate")
        self.imageview.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)
       

2. 

Screen Detail



 Screen Width

1.  Using UIScreen

Code : UIScreen.main.bounds.width

2. Using Frame - Not only screen, It will check view like - button textview too.

Code - view.frame.width


3. Size

Code : view.frame.size.width

4.

 Screen Height

1.  Using UIScreen

Code : UIScreen.main.bounds.height

2. Using Frame - Not only screen, It will check view like - button textview too.

Code - view.frame. height


3. Size

Code : view.frame.size. height

4. Device Screen Middle
Horizontal


UIScreen.main.bounds.midX
Vertical


UIScreen.main.bounds.midY

5. Device Screen Max
Horizontal


Code : UIScreen.main.bounds.maxX
Vertical


Code : UIScreen.main.bounds.maxY

6. Device Screen Min
Horizontal


Code : UIScreen.main.bounds.minX
Vertical


Code : UIScreen.main.bounds.minY



Device Model according to Screen Size

1. 

Code : Call in ViewDidLoad - print("screenType:", UIDevice.current.screenType.rawValue)



extension UIDevice {
    var iPhoneX: Bool {
        return UIScreen.main.nativeBounds.height == 2436
    }
    var iPhone: Bool {
        return UIDevice.current.userInterfaceIdiom == .phone
    }
    enum ScreenType: String {
        case iPhone4 = "iPhone 4 or iPhone 4S"
        case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
        case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
        case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
        case iPhoneX = "iPhone X"
        case unknown
    }
    var screenType: ScreenType {
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhone4
        case 1136:
            return .iPhones_5_5s_5c_SE
        case 1334:
            return .iPhones_6_6s_7_8
        case 1920, 2208:
            return .iPhones_6Plus_6sPlus_7Plus_8Plus
        case 2436:
            return .iPhoneX
        default:
            return .unknown
        }
    }
}

2



Frame






Definition : The frame rectangle, which describe the view’s location and size in its superview coordinate system. Frame is not applicable with AutoLayout o

Note : CGRect = CGPoint + CGSize

CGRect
1.  Centre the child view inside  parent

Code :  Horizontal - 
imageview.frame = CGRect(x: view.frame.width/2, y: 50, width: imageview.frame.width,               height: imageview.frame.height)

   
vertical - 

imageview.frame = CGRect(x: 50, y: view.frame.height/2, width: imageview.frame.width, height: imageview.frame.height)
    

Note : Child view  start from (0, 0) coordinate

2.  Centre the child view inside parent + centre the child view too.

code :  Horizontal

  imageview.frame = CGRect(x: view.frame.width/2 - imageview.frame.width/2, y: 50, width:            imageview.frame.width, height: imageview.frame.height)

    
Vertical 


   imageview.frame = CGRect(x: 50, y: view.frame.height/2 - imageview.frame.height/2, width:           imageview.frame.width, height: imageview.frame.height)





  
3.  Setting Imagview or view equal to other

Code :  Full -   imageview.frame = view.frame

Half -   imageview.frame = view.frame


Origin

4. Position the view according to x and y coordinate

Code :  X-Axis - imageview.frame.origin.x = 200

Y-Axis - imageview.frame.origin.y = 200

5 . Comparing two CGPoint position in swift - 

Example - One Imageview at CGPoint (0, 0)

A. if CGPoint (0, 0)

code : 
        let point = CGPoint(x: 0, y: 0)
        print(imageview.frame.origin.equalTo(point))
        
Result : True

B. if CGPoint (150, 150)

code : 
        let point = CGPoint(x: 150, y: 150)
        print(imageview.frame.origin.equalTo(point))
        
Result : False


Transform
1. ScaleBy - Zoom/Scale the view according to X, Y position.

Code : imageview.transform = view.transform.scaledBy(x: 5, y: 5)

2. Rotated - Rotate the view

Code : imageview.transform = view.transform.rotated(by: 5)


3. Concatenating - Adding value to previous one.

Code : let t = CGAffineTransform(translationX: 5, y: 100)



        imageview.transform = view.transform.concatenating(t)

4. Some more

code : 
        imageview.transform.a = 2
        imageview.transform.b = 3
        imageview.transform.c = 2
        imageview.transform.d = 1
        imageview.transform.tx = 10
        imageview.transform.ty = 10
        

CGAffline

1. Zoom/Scale view

Code : 
        let t = CGAffineTransform(scaleX: 5, y: 5)
        imageview.transform = view.transform.concatenating(t)
        

2. Rotate

Code :   
                let t = CGAffineTransform(rotationAngle: 5)

        imageview.transform = view.transform.concatenating(t)
      
3. Concatenating - Adding value to previous one.

Code : let t = CGAffineTransform(translationX: 5, y: 100)

        imageview.transform = view.transform.concatenating(t)
        












22


Different type to create constraint programmatically

Do you plan to have a squared  UIView  of  width: 100  and  Height: 100  centered inside the  UIView of an  UIViewController ? If so, y...