12

SwiftUI ScaleEffect Tutorial - iOScreator

 4 years ago
source link: https://www.ioscreator.com/tutorials/swiftui-scale-effect-tutorial
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
SwiftUI ScaleEffect Tutorial

In SwiftUI the ScaleEffect modifier can be used to shrink or enlarge the containing view . The content of the view will be altered not the frame size. In this tutorial a text view is displayed for which the scale effect can be changed using sliders and toggles to change the scale value. This tutorial is built for iOS14 and Xcode 12, which can be download at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select App template in the Application section and then click Next.

Enter SwiftUIScaleEffectTutorial as the Product Name, select SwiftUI as Interface, SwiftUI App as Life Cycle and Swift as Language. Deselect the Include Tests checkbos and click Next. Choose a location to save the project on your Mac.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Change the code inside the ContentView struct to

struct ContentView: View {
    // 1.
    @State private var xScaleEffect = 1.0
    @State private var yScaleEffect = 1.0
    @State private var isFlippedVertically = false
    @State private var isFlippedHorizontally = false
    
    var body: some View {
        VStack(spacing: 40) {
            
            Text("ScaleEffect")
                .font(.title)
                // 2.
                .scaleEffect(x: isFlippedVertically ? -1: 1)
                .scaleEffect(y: isFlippedHorizontally ? -1: 1)
                // 3.
                .scaleEffect(x: CGFloat(xScaleEffect))
                .scaleEffect(y: CGFloat(yScaleEffect))
                // 4.  
                .border(Color.red, width: 2)
            // 5.
            Slider(value: $xScaleEffect, in: 0.0...5.0)
            
            Slider(value: $yScaleEffect, in: 0.0...5.0)
            // 6.
            Toggle(isOn: $isFlippedVertically) {
                Text("Flip Vertically")
            }
            
            Toggle(isOn: $isFlippedHorizontally) {
                Text("Flip Vertically")
            }
            
            Spacer()
        
        }
        .padding()
    }
}
  1. The State properties are declared which will hold the scale values and the flipped booleans in both directions.

  2. The ScaleEffect modifier can be applied with negative values to flip the text View.

  3. The scaleEffect contains a x and y value of type CGFloat which applies the scale. The default scale value is 1.0.

  4. The border modifier is used to show the ScaleEffect modifier does not effect the frame size of the text view.

  5. The sliders can be changed to change the x and y scale values

  6. The toggles can be used to flip the text in both directions

Go to the Preview and select the Live Preview button. Change the sliders and toggles to change the scale effect on the text view.

The source code of the SwiftUIScaleeffectTutorial can be downloaded at the ioscreator repository on Github.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK