Search code examples
iosobjective-ccore-graphicscore-animationcalayer

Create a CALayer on each click of a button


I got an IBAction and a CALayer (in the viewDidLoad) so what I need is to add a CALayer (in diffrent CGPoints) when the user click the add button.

Thanks in Advance


Solution

  • I think you are a newbie in iOS, that's why I'm answering the answer, else I'll suggest to use google.

    You can do it like:

    - (IBAction)myButtonClick:(id)sender
    {
        static float xPos = 0;
        static float yPos = 0;
    
        CALayer *myLayer = [CALayer layer];
        [myLayer setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 30.0f)];
        [myLayer setPosition:CGPointMake(xPos, yPos)];   //You need to change the value here dynamically for changing the layer position
        [myLayer setFontSize:20];
        [self.view.layer addSublayer:myLayer];
    
        xPos += 50;
        yPos += 50;
    }