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)
imageview.frame = CGRect(x: 50, y: view.frame.height/2 - imageview.frame.height/2, width: imageview.frame.width, height: imageview.frame.height)
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))
B. if CGPoint (150, 150)
code :
let point = CGPoint(x: 150, y: 150)
print(imageview.frame.origin.equalTo(point))
Transform
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)
Code :
let t = CGAffineTransform(rotationAngle: 5)
imageview.transform = view.transform.concatenating(t)
Code : let t = CGAffineTransform(translationX: 5, y: 100)
imageview.transform = view.transform.concatenating(t)
22
No comments:
Post a Comment