UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

scoping animation

Forums > SwiftUI

I have a View where I use a Picture(image) subview to display an image, which can come in different height and width formats .

The reference to the image is extracted from an array, which allows me to display different images in my View, by varying the reference.

I would like an animation on this image, say a scale effect, when the image is displayed

If I use onAppear(), it is called and performs the scale effect, but only once, on the first image of the array, at the first call of the View .

If I use a Picture(image).scaleEffect(scale).animation() I have an an animation between the first and the second image , varying accordingly height and width which is ugly (and deforms aspectRatio)

If I try Picture(image.animation()) it does not compile.

How can I limit the animation to the scaling effet, and not to the image variation ?

3      

Hopefully this helps with one part of your problem.

https://www.youtube.com/watch?v=Ci78U4nPmrs

3      

Actually no, as far as I can see. The problem I have is not to impose aspectRatio to my 2 images. They both are correct; It is the animation from one to the other which is buggy. i would like to replace the second one with the first, with no animation, then animate the second with a scale effect. I haven't found so far how to separate the two phases in SwiftUI.

3      

I put some code to formalize my problem. I have two images, star and circle. When I clicked on "tap me" the images are updated. What I would like: that the animation only animates the new image. i.e the star replaces the circle, then grows. And if I click again, the circle replaces the star then grows. It doesn't work.

import SwiftUI

let star = Image(systemName: "star.fill")
let circle = Image(systemName: "circle.fill")

struct ContentView: View {
    @State var modified = false
    @State var scale: CGFloat = 0.7

    var body: some View {
         return VStack() {

            Picture(bool: $modified.animation())
                .frame(width: 100, height: 100)
                .animation(nil)
                .scaleEffect(scale)
                .animation(.easeInOut(duration: 2))

           Button(action: {
            self.modified.toggle()
            self.scale = 0.7
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5)
                {self.scale = 1}

            }) {
                Text("Tap here")
                    .animation(.linear)
            }

         }
        }
}

struct Picture: View {
  @Binding var bool: Bool

    var body: some View {
    if bool == true {
        return  star
            .resizable().foregroundColor(.green)
 //
    } else {
       return  circle
            .resizable().foregroundColor(.red)
    }
    }
}

3      

Is this what you were looking for?


import SwiftUI
import PlaygroundSupport

let star = Image(systemName: "star.fill").resizable().aspectRatio(contentMode: .fit)

let circle = Image(systemName: "circle.fill").resizable().aspectRatio(contentMode: .fit)

struct ContentView: View {
    @State var modified = false
    @State var scale: CGFloat = 0.7

    var body: some View {
         return GeometryReader { geo in
            VStack {
                Picture(bool: self.modified)

                    .frame(width: geo.size.width * self.scale)
                    .background(Color.gray)

                Button(action: {
                    self.scale = 0.7
                    self.modified.toggle()
                    withAnimation(.easeIn(duration: 5.5)){
                        self.scale = 1

                    }

                }) {
                    Text("Tap here")
                }}
         }
        }
}

struct Picture: View {
    var bool: Bool

    var body: some View {
    if bool == true {
        return  star
            .foregroundColor(.green)

 //
    } else {
       return  circle
            .foregroundColor(.red)
    }
    }
}

// Present the view controller in the Live View window
let cv = ContentView()

PlaygroundPage.current.setLiveView(cv)

3      

The short answer is Yes ! Thank you so much

The long answer is

  • some minor adaptations

P.S : (this code does not compile in my playground: error: MacPlayground.playground:5:12: error: 'init(systemName:)' is unavailable in macOS let star = Image(systemName: "star.fill").resizable().aspectRatio(contentMode: .fit)

And it does not either in the canvas "Property definition has inferred type 'some View', involving the 'some' return type of another declaration"

but it works with real images, once the .resizable().aspectRatio(contentMode: .fit) put back in the Picture struct

I put it back below for anyone interested )

  • it works for the simplified case I formalized in the code above, it does not work for my real code where the change of image is not explicit with a button requesting it, but implicit from an automatic update of the image in this view triggered by a change in another view. Which means I don't see yet where I should put the action code in the button, since I don't have any button left in this view. I have to experiment to see if I can find the answer by myself, before maybe clarifying in a follow-up
import SwiftUI

let portrait = Image("head")
let landscape = Image("sea")

struct ContentView: View {
    @State var modified = false
    @State var scale: CGFloat = 0.95

    var body: some View {
        VStack(){

        GeometryReader { geo in
            VStack {
                Picture(bool: self.modified)
                    .frame(width: geo.size.width * self.scale)
            }
            }
          Spacer()
          Button(action: {
                    self.scale = 0.95
                    self.modified.toggle()

            withAnimation(.easeInOut(duration: 0.5)){
                        self.scale = 1

                    }
          }) {
                    Text("Tap here")
              }
        }
    }
}

struct Picture: View {
    var bool: Bool

    var body: some View {
    if bool == true {
        return  portrait
            .resizable().aspectRatio(contentMode: .fit)
            .padding(.all,6.0)
    } else {
       return  landscape
            .resizable().aspectRatio(contentMode: .fit)
            .padding(.all,6.0)
    }
    }
}

3      

Thank you for the PS @HugoInParis. I was playing on my IPad. I will try to take out machine/OS specific stuff in future.

Keep up the good work!

3      

Finally I decided to put the needed code in the other view. It feels awkward to put in view B code to animate view A when both are never present at the same time, but it works

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.