Resize radius from 0 to 100 with CABasicAnimation











up vote
1
down vote

favorite












I want to animate a circle. The circle radius should grow from 0 to 100. I tried it with the transform.scale animation. But of course it is not possible to scale a circle with a radius of 0. When I set the radius of the circle to 1 the circle is visible although it shouldn't be at the beginning of the animation.
minimal example:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0
circleAnimation.toValue = 100
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)


At the end of the animation the radius should stay at the new value.



EDIT:
Thanks to @Robiniuß mayoff



Final code:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 400, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0.0000001
circleAnimation.toValue = 1
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)
circleAnimation.fillMode = .backwards









share|improve this question
























  • I'm definitely no expert - at CoreAnimation or math :-) - but can you "fake" it? The difference of 0-1 (or 0-100 versus 1-100) is small... maybe do 1-100 and at the beginning make it appear as such? A UIBezierPath needs a UIView, right? Start the animation by making the view visible?
    – dfd
    Nov 8 at 20:28















up vote
1
down vote

favorite












I want to animate a circle. The circle radius should grow from 0 to 100. I tried it with the transform.scale animation. But of course it is not possible to scale a circle with a radius of 0. When I set the radius of the circle to 1 the circle is visible although it shouldn't be at the beginning of the animation.
minimal example:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0
circleAnimation.toValue = 100
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)


At the end of the animation the radius should stay at the new value.



EDIT:
Thanks to @Robiniuß mayoff



Final code:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 400, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0.0000001
circleAnimation.toValue = 1
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)
circleAnimation.fillMode = .backwards









share|improve this question
























  • I'm definitely no expert - at CoreAnimation or math :-) - but can you "fake" it? The difference of 0-1 (or 0-100 versus 1-100) is small... maybe do 1-100 and at the beginning make it appear as such? A UIBezierPath needs a UIView, right? Start the animation by making the view visible?
    – dfd
    Nov 8 at 20:28













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to animate a circle. The circle radius should grow from 0 to 100. I tried it with the transform.scale animation. But of course it is not possible to scale a circle with a radius of 0. When I set the radius of the circle to 1 the circle is visible although it shouldn't be at the beginning of the animation.
minimal example:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0
circleAnimation.toValue = 100
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)


At the end of the animation the radius should stay at the new value.



EDIT:
Thanks to @Robiniuß mayoff



Final code:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 400, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0.0000001
circleAnimation.toValue = 1
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)
circleAnimation.fillMode = .backwards









share|improve this question















I want to animate a circle. The circle radius should grow from 0 to 100. I tried it with the transform.scale animation. But of course it is not possible to scale a circle with a radius of 0. When I set the radius of the circle to 1 the circle is visible although it shouldn't be at the beginning of the animation.
minimal example:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0
circleAnimation.toValue = 100
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)


At the end of the animation the radius should stay at the new value.



EDIT:
Thanks to @Robiniuß mayoff



Final code:



let circleShape = CAShapeLayer()

let circlePath = UIBezierPath(arcCenter: .zero, radius: 400, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0.0000001
circleAnimation.toValue = 1
circleAnimation.duration = 1.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)
circleAnimation.fillMode = .backwards






swift swift4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 20:49

























asked Nov 8 at 19:03









ReasyEasyPeasy

4492621




4492621












  • I'm definitely no expert - at CoreAnimation or math :-) - but can you "fake" it? The difference of 0-1 (or 0-100 versus 1-100) is small... maybe do 1-100 and at the beginning make it appear as such? A UIBezierPath needs a UIView, right? Start the animation by making the view visible?
    – dfd
    Nov 8 at 20:28


















  • I'm definitely no expert - at CoreAnimation or math :-) - but can you "fake" it? The difference of 0-1 (or 0-100 versus 1-100) is small... maybe do 1-100 and at the beginning make it appear as such? A UIBezierPath needs a UIView, right? Start the animation by making the view visible?
    – dfd
    Nov 8 at 20:28
















I'm definitely no expert - at CoreAnimation or math :-) - but can you "fake" it? The difference of 0-1 (or 0-100 versus 1-100) is small... maybe do 1-100 and at the beginning make it appear as such? A UIBezierPath needs a UIView, right? Start the animation by making the view visible?
– dfd
Nov 8 at 20:28




I'm definitely no expert - at CoreAnimation or math :-) - but can you "fake" it? The difference of 0-1 (or 0-100 versus 1-100) is small... maybe do 1-100 and at the beginning make it appear as such? A UIBezierPath needs a UIView, right? Start the animation by making the view visible?
– dfd
Nov 8 at 20:28












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Probably what you want to do is set the radius of circlePath to your final radius:



let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
circlePath.clone()


And then animate from a near-zero number to 1.0:



circleAnimation.fromValue = 0.0001
circleAnimation.toValue = 1.0


If you want to have the animation start in the future (by setting beginTime), then you also probably want to set the fill mode so that the fromValue is applied to the layer until the animation starts:



circleAnimation.fillMode = .backwards





share|improve this answer





















  • thanks! that worked for me!
    – ReasyEasyPeasy
    Nov 8 at 20:49


















up vote
0
down vote













You should use a radius greater 0. Then you can scale it down with circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
before adding the layer. And then you could start the animation:



        let circleShape = CAShapeLayer()
let center = CGPoint(x: view.frame.width / 2, y: view.frame.height / 2)
let circlePath = UIBezierPath(arcCenter: center, radius: 100.0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.brown.cgColor
let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

circleAnimation.fromValue = 0
circleAnimation.toValue = 1
circleAnimation.duration = 5.9
circleAnimation.beginTime = CACurrentMediaTime() + 1.5
circleShape.add(circleAnimation, forKey: nil)

view.layer.addSublayer(circleShape)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214495%2fresize-radius-from-0-to-100-with-cabasicanimation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Probably what you want to do is set the radius of circlePath to your final radius:



    let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
    circlePath.clone()


    And then animate from a near-zero number to 1.0:



    circleAnimation.fromValue = 0.0001
    circleAnimation.toValue = 1.0


    If you want to have the animation start in the future (by setting beginTime), then you also probably want to set the fill mode so that the fromValue is applied to the layer until the animation starts:



    circleAnimation.fillMode = .backwards





    share|improve this answer





















    • thanks! that worked for me!
      – ReasyEasyPeasy
      Nov 8 at 20:49















    up vote
    1
    down vote



    accepted










    Probably what you want to do is set the radius of circlePath to your final radius:



    let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
    circlePath.clone()


    And then animate from a near-zero number to 1.0:



    circleAnimation.fromValue = 0.0001
    circleAnimation.toValue = 1.0


    If you want to have the animation start in the future (by setting beginTime), then you also probably want to set the fill mode so that the fromValue is applied to the layer until the animation starts:



    circleAnimation.fillMode = .backwards





    share|improve this answer





















    • thanks! that worked for me!
      – ReasyEasyPeasy
      Nov 8 at 20:49













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Probably what you want to do is set the radius of circlePath to your final radius:



    let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
    circlePath.clone()


    And then animate from a near-zero number to 1.0:



    circleAnimation.fromValue = 0.0001
    circleAnimation.toValue = 1.0


    If you want to have the animation start in the future (by setting beginTime), then you also probably want to set the fill mode so that the fromValue is applied to the layer until the animation starts:



    circleAnimation.fillMode = .backwards





    share|improve this answer












    Probably what you want to do is set the radius of circlePath to your final radius:



    let circlePath = UIBezierPath(arcCenter: .zero, radius: 0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
    circlePath.clone()


    And then animate from a near-zero number to 1.0:



    circleAnimation.fromValue = 0.0001
    circleAnimation.toValue = 1.0


    If you want to have the animation start in the future (by setting beginTime), then you also probably want to set the fill mode so that the fromValue is applied to the layer until the animation starts:



    circleAnimation.fillMode = .backwards






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 20:42









    rob mayoff

    288k40578634




    288k40578634












    • thanks! that worked for me!
      – ReasyEasyPeasy
      Nov 8 at 20:49


















    • thanks! that worked for me!
      – ReasyEasyPeasy
      Nov 8 at 20:49
















    thanks! that worked for me!
    – ReasyEasyPeasy
    Nov 8 at 20:49




    thanks! that worked for me!
    – ReasyEasyPeasy
    Nov 8 at 20:49












    up vote
    0
    down vote













    You should use a radius greater 0. Then you can scale it down with circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
    before adding the layer. And then you could start the animation:



            let circleShape = CAShapeLayer()
    let center = CGPoint(x: view.frame.width / 2, y: view.frame.height / 2)
    let circlePath = UIBezierPath(arcCenter: center, radius: 100.0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
    circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

    circleShape.path = circlePath.cgPath
    circleShape.fillColor = UIColor.brown.cgColor
    let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

    circleAnimation.fromValue = 0
    circleAnimation.toValue = 1
    circleAnimation.duration = 5.9
    circleAnimation.beginTime = CACurrentMediaTime() + 1.5
    circleShape.add(circleAnimation, forKey: nil)

    view.layer.addSublayer(circleShape)





    share|improve this answer

























      up vote
      0
      down vote













      You should use a radius greater 0. Then you can scale it down with circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
      before adding the layer. And then you could start the animation:



              let circleShape = CAShapeLayer()
      let center = CGPoint(x: view.frame.width / 2, y: view.frame.height / 2)
      let circlePath = UIBezierPath(arcCenter: center, radius: 100.0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
      circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

      circleShape.path = circlePath.cgPath
      circleShape.fillColor = UIColor.brown.cgColor
      let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

      circleAnimation.fromValue = 0
      circleAnimation.toValue = 1
      circleAnimation.duration = 5.9
      circleAnimation.beginTime = CACurrentMediaTime() + 1.5
      circleShape.add(circleAnimation, forKey: nil)

      view.layer.addSublayer(circleShape)





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You should use a radius greater 0. Then you can scale it down with circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
        before adding the layer. And then you could start the animation:



                let circleShape = CAShapeLayer()
        let center = CGPoint(x: view.frame.width / 2, y: view.frame.height / 2)
        let circlePath = UIBezierPath(arcCenter: center, radius: 100.0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
        circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

        circleShape.path = circlePath.cgPath
        circleShape.fillColor = UIColor.brown.cgColor
        let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

        circleAnimation.fromValue = 0
        circleAnimation.toValue = 1
        circleAnimation.duration = 5.9
        circleAnimation.beginTime = CACurrentMediaTime() + 1.5
        circleShape.add(circleAnimation, forKey: nil)

        view.layer.addSublayer(circleShape)





        share|improve this answer












        You should use a radius greater 0. Then you can scale it down with circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
        before adding the layer. And then you could start the animation:



                let circleShape = CAShapeLayer()
        let center = CGPoint(x: view.frame.width / 2, y: view.frame.height / 2)
        let circlePath = UIBezierPath(arcCenter: center, radius: 100.0, startAngle: CGFloat(0), endAngle: CGFloat(Double.pi*2), clockwise: true)
        circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

        circleShape.path = circlePath.cgPath
        circleShape.fillColor = UIColor.brown.cgColor
        let circleAnimation = CABasicAnimation(keyPath: "transform.scale")

        circleAnimation.fromValue = 0
        circleAnimation.toValue = 1
        circleAnimation.duration = 5.9
        circleAnimation.beginTime = CACurrentMediaTime() + 1.5
        circleShape.add(circleAnimation, forKey: nil)

        view.layer.addSublayer(circleShape)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 20:47









        Stephan Januar

        713




        713






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214495%2fresize-radius-from-0-to-100-with-cabasicanimation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff