Thursday, June 2, 2016

How to Scale Objects

Words

transform

- You set a UIView's transform property to scale it. The view is transformed using mathematical formulas in an object that you supply. In this case, you are supplying a CGAffineTransformMakeScale object with two parameters.

Affine

- Means to preserve parallel relationships. When you transform a view you might be scaling it (the sides of the view are still parallel to each other). You may also be rotating the view in which the sides are still parallel to each other.

CGAffineTransformMakeScale

- This object will hold the values you supply and apply the correct transformation to scale the view. Takes two parameters:
  • Scale X (sx) - A number by which you want to scale the width.
  • Scale Y (sy) - A number by which you want to scale the height.
If you provide a 2 that means you want the view to be twice the width/height.
If you provide a 1 that just means you want the width/height to be the original size.
A number lower than 1 will shrink the size of the view.

Examples

 
class ViewController: UIViewController {
    
    @IBOutlet weak var greenView: UIView!
    
    @IBAction func biggerButton(sender: AnyObject) {
        // Scale the view to be twice as big.
        greenView.transform = CGAffineTransformMakeScale(2, 2)
    }
    
    @IBAction func resetButton(sender: AnyObject) {
        // Reset the view to be normal size.
        greenView.transform = CGAffineTransformMakeScale(1, 1)
    }
    
    @IBAction func halfSizeButton(sender: AnyObject) {
        // Scale the view to be half the original size.
        greenView.transform = CGAffineTransformMakeScale(0.5, 0.5)
    }
    
    @IBAction func TwiceTall(sender: AnyObject) {
        // Keep width 1x, make height 2x.
        greenView.transform = CGAffineTransformMakeScale(1, 2)
    }
    
    @IBAction func TwiceWide(sender: AnyObject) {
        // Make width 2x, make height 1x.
        greenView.transform = CGAffineTransformMakeScale(2, 1)
    }

    @IBAction func HideButton(sender: AnyObject) {
        // Scaling to zero hides the view.
        greenView.transform = CGAffineTransformMakeScale(0, 0)
    }
}
 

(Swift 2.2)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...