CGRect syntax I haven't seen before

前端 未结 3 1672
离开以前
离开以前 2021-01-30 17:49

I saw the syntax below in some example code and am not sure I understand it.

 CGRect imageRect = (CGRect){.size = baseImage.size};

Is this simp

3条回答
  •  无人及你
    2021-01-30 18:34

    This is not just shorthand syntax but is also useful when you want to change only the size and not the origin in CGRect and vice versa.

    Eg : I want to change only the size and the position has a complicated syntax and I dont want to change it. Noramlly, I would do

    CGRect imageRect = CGRectMake(sprite.origin.x,sprite.origin.y,40, 60);
    

    With the other syntax i would do

    CGRect imageRect = (CGRect){.size = sprite.size};
    

    also we can directy use add, subtract and multiply methods eg.

     CGRect imageRect = (CGRect){.size = ccpAdd(sprite.size,addsize)};
    

    Hope this helps

提交回复
热议问题