Objective:
Learning iOS development for some time, the project has done two, see the video today, suddenly found that the view frame and bound two properties, found that bound how also want to not understand, as if spared you a dead end, after a try and think, finally understand the meaning of bound. PS: I admit that I am a very stupid and stupid person.
So it is now recorded for later inspection, at the same time convenient for all and I have the same doubts of the people to view.
First, make a list of accepted information:
See the following code first you must understand something:
-(CGRect) frame{
return CGRectMake (self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}
-(CGRect) bounds{
return CGRectMake (0,0,self.frame.size.width,self.frame.size.height);
}
Obviously, the origin of the bounds is (0,0) point (which is the coordinate system of the view itself, which is always 0, 0, unless considered setbounds), and the frame's origin is arbitrary (relative to the coordinate position in the parent view).
Look at sketch and you'll see.
Frame: The position and size of the view in the parent view coordinate system. (reference point is, Father's coordinate system)
Bounds: The position and size of the view in the local coordinate system. (The reference point is that the local coordinate system is equivalent to VIEWB's own coordinate system, starting at 0, 0 points)
Center: The position and size of the view's central point in the parent view coordinate system. (Reference electricity is, father's coordinate system)
Personally, I think, bounds a little puzzled, a little inattentive, think more, will go around. Each view has a local coordinate system. This coordinate system function is more important, such as the touch of the callback function in the Uitouch inside the > Coordinate values are reference to the local coordinate system coordinates. Of course bounds this attribute is also referred to this local coordinate system. In fact, the key of the local coordinate system is to know where its origin (0,0) is located (this position is relative to the upper view of the local coordinate system, of course, the top layer of view is the window its local coordinate system origin is the upper left corner of the screen). The local coordinate system's origin location can be modified by modifying the view's Bounds property.
So, I personally think that bounds affects the location and size of the child view.
Second, demo Demo:
[CPP]View Plaincopy
- UIView *view1 = [[UIView alloc] Initwithframe:cgrectmake (20, 20, 280, 250)];
- [View1 Setbounds:cgrectmake (-20,-20, 280, 250)];
- View1.backgroundcolor = [Uicolor Redcolor];
- [Self.view Addsubview:view1]; //Add to Self.view
- NSLog (@"View1 frame:%@========view1 bounds:%@", Nsstringfromcgrect (View1.frame), Nsstringfromcgrect ( View1.bounds));
- UIView *view2 = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 100, 100)];
- View2.backgroundcolor = [Uicolor Yellowcolor];
- [View1 ADDSUBVIEW:VIEW2]; ///Add to View1, [the upper-left corner of the View1 coordinate system starts at ( -20,-20)]
- NSLog (@"View2 frame:%@========view2 bounds:%@", Nsstringfromcgrect (View2.frame), Nsstringfromcgrect ( View2.bounds));
(Run the show, the picture said very clearly OH)
(log output logs indicate that each new view default bounds is actually (0,0))
The difference between frame and bounds of iOS view (location and size)