Objective c 将CGRect分解为4个变量
假设drawRect接受一个CGRect'rect',我如何创建4个单独的CGFloat变量,每个变量的值为rect x、y、width和height?我需要单独使用变量来确定drawRect方法中的某些绘图比例Objective c 将CGRect分解为4个变量,objective-c,Objective C,假设drawRect接受一个CGRect'rect',我如何创建4个单独的CGFloat变量,每个变量的值为rect x、y、width和height?我需要单独使用变量来确定drawRect方法中的某些绘图比例 - (void)drawRect:(CGRect)rect { CGFLoat x = ***x value of rect***; CGFLoat y = ***y value of rect***; CGFloat width = ***width value of rect
- (void)drawRect:(CGRect)rect {
CGFLoat x = ***x value of rect***;
CGFLoat y = ***y value of rect***;
CGFloat width = ***width value of rect***;
CGFloat height = ***height value of rect***;
}
提前感谢您的帮助 CGRect的结构就是这样定义的
CGFLoat x = rect.origin.x;
CGFLoat y = rect.origin.y;
CGFloat width = rect.size.width;
CGFloat height = rect.size.height;
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
这意味着您可以这样做:
rect.origin.x
rect.origin.y
rect.size.width
rect.size.height
我建议你温习一些基本的C语言材料,因为问题是基本的C语言结构,没什么大不了的——我们都去过一次 鉴于:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
/* Sizes. */
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
/* Rectangles. */
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
你想要:
-voiddrawRect:cGrect{
CGFLoat x=rect.origin.x;
CGFLoat y=矩形原点y;
CGFloat width=rect.size.width;
CGFloat高度=rect.size.height;
}
或者,更好的是,因为这种疯狂有其功能:
- (void)drawRect:(CGRect)rect {
CGFLoat x = CGRectGetMinX(rect);
CGFLoat y = CGRectGetMinY(rect);
CGFloat width = CGRectGetWidth(rect);
CGFloat height = CGRectGetHeight(rect);
}
声明有点不完整,塔尔。假设您知道CGPoint和CGSize是什么。同意,您的答案更完整:非常好,谢谢!我知道我的技能已经过时了,还有很多东西要学-不用担心-就像我说的,我们都去过那里。如果您单击类型,Xcode通常会很好地引导您了解某些内容的定义。