Sunday, 26 November 2017

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


No comments:

Post a Comment

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...