Core Animation is not working with “alpha” value












34














Before this code, my movie pic alpha is set to 0,



CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"alpha"];


Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.



I've seen a code using UIView beginAnimations: around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?










share|improve this question





























    34














    Before this code, my movie pic alpha is set to 0,



    CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
    [fadein setToValue:[NSNumber numberWithFloat:1.0]];
    [fadein setDuration:0.5];
    [[moviepic layer]addAnimation:fadein forKey:@"alpha"];


    Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.



    I've seen a code using UIView beginAnimations: around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?










    share|improve this question



























      34












      34








      34


      6





      Before this code, my movie pic alpha is set to 0,



      CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
      [fadein setToValue:[NSNumber numberWithFloat:1.0]];
      [fadein setDuration:0.5];
      [[moviepic layer]addAnimation:fadein forKey:@"alpha"];


      Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.



      I've seen a code using UIView beginAnimations: around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?










      share|improve this question















      Before this code, my movie pic alpha is set to 0,



      CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
      [fadein setToValue:[NSNumber numberWithFloat:1.0]];
      [fadein setDuration:0.5];
      [[moviepic layer]addAnimation:fadein forKey:@"alpha"];


      Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.



      I've seen a code using UIView beginAnimations: around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?







      ios core-animation alpha cabasicanimation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 14 '13 at 4:43

























      asked Sep 23 '11 at 1:49









      5argon

      1,24811531




      1,24811531
























          3 Answers
          3






          active

          oldest

          votes


















          96














          [CABasicAnimation animationWithKeyPath:@"opacity"];


          UIView exposes this as alpha where as CALayer exposes this as opacity.






          share|improve this answer























          • What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
            – 5argon
            Sep 23 '11 at 16:30








          • 4




            @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
            – Palimondo
            Sep 27 '11 at 12:23





















          1














          @ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation please refer to Apple's documentation






          share|improve this answer





























            1














            For Swift:



            let opacity = CABasicAnimation(keyPath: "opacity")
            opacity.fromValue = fromValue
            opacity.toValue = toValue
            opacity.duration = duration
            opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed

            view.layer.add(opacity, forKey: nil)


            If you want to keep the final alpha value, you have to set the current view controller as the delegate of the opacity animation:



            opacity.delegate = self


            And, in the delegate function animationDidStop, you should do:



            extension ViewController: CAAnimationDelegate {

            func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
            view.alpha = toValue
            }
            }





            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',
              autoActivateHeartbeat: false,
              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%2f7523441%2fcore-animation-is-not-working-with-alpha-value%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              96














              [CABasicAnimation animationWithKeyPath:@"opacity"];


              UIView exposes this as alpha where as CALayer exposes this as opacity.






              share|improve this answer























              • What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
                – 5argon
                Sep 23 '11 at 16:30








              • 4




                @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
                – Palimondo
                Sep 27 '11 at 12:23


















              96














              [CABasicAnimation animationWithKeyPath:@"opacity"];


              UIView exposes this as alpha where as CALayer exposes this as opacity.






              share|improve this answer























              • What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
                – 5argon
                Sep 23 '11 at 16:30








              • 4




                @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
                – Palimondo
                Sep 27 '11 at 12:23
















              96












              96








              96






              [CABasicAnimation animationWithKeyPath:@"opacity"];


              UIView exposes this as alpha where as CALayer exposes this as opacity.






              share|improve this answer














              [CABasicAnimation animationWithKeyPath:@"opacity"];


              UIView exposes this as alpha where as CALayer exposes this as opacity.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 23 '11 at 4:24









              Evan

              4,96111843




              4,96111843










              answered Sep 23 '11 at 3:12









              ohho

              29.7k59214347




              29.7k59214347












              • What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
                – 5argon
                Sep 23 '11 at 16:30








              • 4




                @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
                – Palimondo
                Sep 27 '11 at 12:23




















              • What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
                – 5argon
                Sep 23 '11 at 16:30








              • 4




                @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
                – Palimondo
                Sep 27 '11 at 12:23


















              What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
              – 5argon
              Sep 23 '11 at 16:30






              What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
              – 5argon
              Sep 23 '11 at 16:30






              4




              4




              @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
              – Palimondo
              Sep 27 '11 at 12:23






              @Sargon With Core Animation you are animating CALayer properties, not UIView properties, so the layer is the place to look for correct property names - see: view.layer.
              – Palimondo
              Sep 27 '11 at 12:23















              1














              @ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation please refer to Apple's documentation






              share|improve this answer


























                1














                @ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation please refer to Apple's documentation






                share|improve this answer
























                  1












                  1








                  1






                  @ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation please refer to Apple's documentation






                  share|improve this answer












                  @ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation please refer to Apple's documentation







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 28 '15 at 10:57









                  Julian Król

                  7,82043759




                  7,82043759























                      1














                      For Swift:



                      let opacity = CABasicAnimation(keyPath: "opacity")
                      opacity.fromValue = fromValue
                      opacity.toValue = toValue
                      opacity.duration = duration
                      opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed

                      view.layer.add(opacity, forKey: nil)


                      If you want to keep the final alpha value, you have to set the current view controller as the delegate of the opacity animation:



                      opacity.delegate = self


                      And, in the delegate function animationDidStop, you should do:



                      extension ViewController: CAAnimationDelegate {

                      func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
                      view.alpha = toValue
                      }
                      }





                      share|improve this answer




























                        1














                        For Swift:



                        let opacity = CABasicAnimation(keyPath: "opacity")
                        opacity.fromValue = fromValue
                        opacity.toValue = toValue
                        opacity.duration = duration
                        opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed

                        view.layer.add(opacity, forKey: nil)


                        If you want to keep the final alpha value, you have to set the current view controller as the delegate of the opacity animation:



                        opacity.delegate = self


                        And, in the delegate function animationDidStop, you should do:



                        extension ViewController: CAAnimationDelegate {

                        func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
                        view.alpha = toValue
                        }
                        }





                        share|improve this answer


























                          1












                          1








                          1






                          For Swift:



                          let opacity = CABasicAnimation(keyPath: "opacity")
                          opacity.fromValue = fromValue
                          opacity.toValue = toValue
                          opacity.duration = duration
                          opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed

                          view.layer.add(opacity, forKey: nil)


                          If you want to keep the final alpha value, you have to set the current view controller as the delegate of the opacity animation:



                          opacity.delegate = self


                          And, in the delegate function animationDidStop, you should do:



                          extension ViewController: CAAnimationDelegate {

                          func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
                          view.alpha = toValue
                          }
                          }





                          share|improve this answer














                          For Swift:



                          let opacity = CABasicAnimation(keyPath: "opacity")
                          opacity.fromValue = fromValue
                          opacity.toValue = toValue
                          opacity.duration = duration
                          opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed

                          view.layer.add(opacity, forKey: nil)


                          If you want to keep the final alpha value, you have to set the current view controller as the delegate of the opacity animation:



                          opacity.delegate = self


                          And, in the delegate function animationDidStop, you should do:



                          extension ViewController: CAAnimationDelegate {

                          func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
                          view.alpha = toValue
                          }
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 20 '18 at 8:09

























                          answered Nov 19 '18 at 12:05









                          Ginés SM

                          633




                          633






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7523441%2fcore-animation-is-not-working-with-alpha-value%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

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              SQL update select statement

                              'app-layout' is not a known element: how to share Component with different Modules