Why does the original layer lose its mask when being copied?

Multi tool use
Multi tool use












0















When I try to copy an object from a Custom Layer Class I have created, the copy created matches the original, but the original loses its mask.



In order to perform the copy, I have created an extension of my custom class:



extension CustomLayer: NSCopying {

func copy(with zone: NSZone? = nil) -> Any {
let clone = CustomLayer()

// Type Properties
clone.colorFill = self.colorFill
clone.colorMap = self.colorMap
clone.radius = self.radius

// LayerProperties
clone.frame = self.frame
clone.cornerRadius = self.cornerRadius
clone.opacity = self.opacity
clone.position = self.position

clone.shadowRadius = self.shadowRadius
clone.shadowColor = self.shadowColor
clone.shadowOffset = self.shadowOffset
clone.shadowOpacity = self.shadowOpacity
clone.shadowPath = self.shadowPath

clone.mask = self.mask

return clone
}
}


I then create a copy this way:



let originalLayer = CustomLayer()
...
view.layer.addSublayer(originalLayer)

let copyLayer = originalLayer.copy() as! CustomLayer
view.layer.addSublayer(copyLayer)


As you can see from the screenshot, the original layer does not have a mask anymore whilst the copy has it.



Top: original layer / Bottom: copy layer



I have used this method in the past for other Custom Layer Class and I have never encountered this issue until now. I have also tried create a CALayer that is an instance of self.mask and then applies it as a mask to the copy, but the result is the same.



Any pointers would be greatly appreciated!



Thanks in advance for your time!










share|improve this question



























    0















    When I try to copy an object from a Custom Layer Class I have created, the copy created matches the original, but the original loses its mask.



    In order to perform the copy, I have created an extension of my custom class:



    extension CustomLayer: NSCopying {

    func copy(with zone: NSZone? = nil) -> Any {
    let clone = CustomLayer()

    // Type Properties
    clone.colorFill = self.colorFill
    clone.colorMap = self.colorMap
    clone.radius = self.radius

    // LayerProperties
    clone.frame = self.frame
    clone.cornerRadius = self.cornerRadius
    clone.opacity = self.opacity
    clone.position = self.position

    clone.shadowRadius = self.shadowRadius
    clone.shadowColor = self.shadowColor
    clone.shadowOffset = self.shadowOffset
    clone.shadowOpacity = self.shadowOpacity
    clone.shadowPath = self.shadowPath

    clone.mask = self.mask

    return clone
    }
    }


    I then create a copy this way:



    let originalLayer = CustomLayer()
    ...
    view.layer.addSublayer(originalLayer)

    let copyLayer = originalLayer.copy() as! CustomLayer
    view.layer.addSublayer(copyLayer)


    As you can see from the screenshot, the original layer does not have a mask anymore whilst the copy has it.



    Top: original layer / Bottom: copy layer



    I have used this method in the past for other Custom Layer Class and I have never encountered this issue until now. I have also tried create a CALayer that is an instance of self.mask and then applies it as a mask to the copy, but the result is the same.



    Any pointers would be greatly appreciated!



    Thanks in advance for your time!










    share|improve this question

























      0












      0








      0








      When I try to copy an object from a Custom Layer Class I have created, the copy created matches the original, but the original loses its mask.



      In order to perform the copy, I have created an extension of my custom class:



      extension CustomLayer: NSCopying {

      func copy(with zone: NSZone? = nil) -> Any {
      let clone = CustomLayer()

      // Type Properties
      clone.colorFill = self.colorFill
      clone.colorMap = self.colorMap
      clone.radius = self.radius

      // LayerProperties
      clone.frame = self.frame
      clone.cornerRadius = self.cornerRadius
      clone.opacity = self.opacity
      clone.position = self.position

      clone.shadowRadius = self.shadowRadius
      clone.shadowColor = self.shadowColor
      clone.shadowOffset = self.shadowOffset
      clone.shadowOpacity = self.shadowOpacity
      clone.shadowPath = self.shadowPath

      clone.mask = self.mask

      return clone
      }
      }


      I then create a copy this way:



      let originalLayer = CustomLayer()
      ...
      view.layer.addSublayer(originalLayer)

      let copyLayer = originalLayer.copy() as! CustomLayer
      view.layer.addSublayer(copyLayer)


      As you can see from the screenshot, the original layer does not have a mask anymore whilst the copy has it.



      Top: original layer / Bottom: copy layer



      I have used this method in the past for other Custom Layer Class and I have never encountered this issue until now. I have also tried create a CALayer that is an instance of self.mask and then applies it as a mask to the copy, but the result is the same.



      Any pointers would be greatly appreciated!



      Thanks in advance for your time!










      share|improve this question














      When I try to copy an object from a Custom Layer Class I have created, the copy created matches the original, but the original loses its mask.



      In order to perform the copy, I have created an extension of my custom class:



      extension CustomLayer: NSCopying {

      func copy(with zone: NSZone? = nil) -> Any {
      let clone = CustomLayer()

      // Type Properties
      clone.colorFill = self.colorFill
      clone.colorMap = self.colorMap
      clone.radius = self.radius

      // LayerProperties
      clone.frame = self.frame
      clone.cornerRadius = self.cornerRadius
      clone.opacity = self.opacity
      clone.position = self.position

      clone.shadowRadius = self.shadowRadius
      clone.shadowColor = self.shadowColor
      clone.shadowOffset = self.shadowOffset
      clone.shadowOpacity = self.shadowOpacity
      clone.shadowPath = self.shadowPath

      clone.mask = self.mask

      return clone
      }
      }


      I then create a copy this way:



      let originalLayer = CustomLayer()
      ...
      view.layer.addSublayer(originalLayer)

      let copyLayer = originalLayer.copy() as! CustomLayer
      view.layer.addSublayer(copyLayer)


      As you can see from the screenshot, the original layer does not have a mask anymore whilst the copy has it.



      Top: original layer / Bottom: copy layer



      I have used this method in the past for other Custom Layer Class and I have never encountered this issue until now. I have also tried create a CALayer that is an instance of self.mask and then applies it as a mask to the copy, but the result is the same.



      Any pointers would be greatly appreciated!



      Thanks in advance for your time!







      ios calayer mask deep-copy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 0:56









      ChrisChris

      266




      266
























          1 Answer
          1






          active

          oldest

          votes


















          0














          My intuition tells me that you probably have to copy the mask in your copy implementation. I found this question that supports my intuition.



          Can a single CALayer be used as the mask for multiple other layers?






          share|improve this answer
























          • Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

            – Chris
            Dec 30 '18 at 10:30











          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%2f53974513%2fwhy-does-the-original-layer-lose-its-mask-when-being-copied%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          My intuition tells me that you probably have to copy the mask in your copy implementation. I found this question that supports my intuition.



          Can a single CALayer be used as the mask for multiple other layers?






          share|improve this answer
























          • Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

            – Chris
            Dec 30 '18 at 10:30
















          0














          My intuition tells me that you probably have to copy the mask in your copy implementation. I found this question that supports my intuition.



          Can a single CALayer be used as the mask for multiple other layers?






          share|improve this answer
























          • Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

            – Chris
            Dec 30 '18 at 10:30














          0












          0








          0







          My intuition tells me that you probably have to copy the mask in your copy implementation. I found this question that supports my intuition.



          Can a single CALayer be used as the mask for multiple other layers?






          share|improve this answer













          My intuition tells me that you probably have to copy the mask in your copy implementation. I found this question that supports my intuition.



          Can a single CALayer be used as the mask for multiple other layers?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 30 '18 at 1:05









          Joe VBJoe VB

          213




          213













          • Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

            – Chris
            Dec 30 '18 at 10:30



















          • Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

            – Chris
            Dec 30 '18 at 10:30

















          Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

          – Chris
          Dec 30 '18 at 10:30





          Thanks for your reply @Joe . In the thread you indicate, it says to use the same CA Layer as masks for multiple layers, therefore I can see why it does not work. Here I am already inside a copy method and I fail to connect the dots. Furthermore as soon as I use self.mask even if I do not attach it to the clone layer mask (let's say I create another CALayer and sets it to self.mask), the original copy sets its mask to nil. Last but not least, the copy method I use works for other custom classes whereas according to the thread it then should not work at all... Hence why I am puzzled.

          – Chris
          Dec 30 '18 at 10:30


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974513%2fwhy-does-the-original-layer-lose-its-mask-when-being-copied%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







          EQ4C9 zMjH,sJg5Wm,VCHzPr al,k,FatzTxhvnR0bImtnwjcGbR,m7Ii TGH2HC zyk u,i4NlvVPpys,ri,jjFV,RAx YsB6ATUOrzqm9yI
          nqbWBl,LDJ1xR 42wHNrWtyFp79CmZ1yVG4hs6Ij4PO,sDNO8x arhIbBPaXw5QBtgyA5G1eHBli7iWchL6tp2O,CYbs2YHoQ2pI07,5

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas