myView.frame.origin.x = value; does not work - But why?

前端 未结 4 1659
北恋
北恋 2020-12-02 16:06

I know that I can\'t use this:

myView.frame.origin.x = 25.0;

and that I have to use this instead:

CGRect myFrame = myView.f         


        
4条回答
  •  渐次进展
    2020-12-02 16:15

    When you manipulate the data directly, no accessor is called, so the UI cannot update itself - or inform any other component that wants to know about changes.

    Edit: As pointed out by walkytalky, you will get a copy of the data, so changing it doesn't have any effect on the original anyway. The following example will show this:

    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(50,50,100,100)];
    NSLog(@"%f", aView.frame.origin.x); // will give 50
    aView.frame.origin.x = 17;  // operates on a copy of the rect only
    NSLog(@"%f", aView.frame.origin.x);  // will still give 50
    

提交回复
热议问题