Reputation: 6445
I have a variable CGFloat x
in my view controller. I created a frame with this variable:
CGRectMake(x,x,4*x,4*x)
When x
is changed, I want the value of frame to also change without any manual updating.
How can I do that?
Upvotes: 1
Views: 797
Reputation: 9178
There is no way to 'deep copy' a CGFloat
, since CGFloat
is a primitive type, not an object. Additionally, CGRect
is a struct
, not an object, so it consists of 4 CGFloat
s stored one after the other. Not references to CGFloat
s, but the raw values themselves.
However, if you declare x
as a property of your view controller:
@property CGFloat x;
Then you can override the setter of x
to update the value of your frame:
- (void)setX:(CGFloat)x {
_x = x;
self.myFrame = CGRectMake(x, x, x * 4, x * 4);
}
In this way, every time you change x
using self.x = whatever
, the setter method will be called and your frame will be updated.
This is basically the safest and most reliable way to do this, as long as you remember to only set x
by self.x = whatever
, if you set the underlying ivar _x
directly, the setter will not be called and the frame won't update.
Upvotes: 3
Reputation: 17535
You can't make a copy of Immutable data.So you can't make a copy CGFloat.But if you wanna change your frame in that case you need to set your frame in setter method of X property.
Upvotes: 1