绘图 - 3

来源:互联网 发布:国家顶级域名是 编辑:程序博客网 时间:2024/06/10 21:47

绘图这块,我慢慢地会整理一番,前期肯定会比较混乱,毕竟知识面太广泛了。


好文章:http://gypgyp.iteye.com/blog/1552067


现在主要为集成一些涉及到的方法和类及实现。

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;

UIView的两个方法,十分有意思,首先要对一个概念熟悉,Coordinate system ,相信有初中的知识就可以够用了。而这个方法顾名思义,就是将一个点从基于的一个坐标系转换到基于另外一个的坐标系。

http://stackoverflow.com/questions/8465659/understand-convertrecttoview-convertrectfromview-convertpointtoview-and

这里给出的一个例子,但是上面需要一点地修改是point参数其实最好给出常数,这里的例子给了view1.bounds.origin,其实就是{ 0 , 0 }这个数,只是很容易让人误解这个参数的意义。这个point 的意义就是偏移值

    UIView* containView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];    [containView setBackgroundColor:[UIColor lightGrayColor]];    [self.view addSubview:containView];            UILabel* label = [[UILabel alloc] init];    [label setFrame:CGRectMake(50, 50, 100, 100)];    [label setBackgroundColor:[UIColor yellowColor]];    [containView addSubview:label];        CGPoint originInContainViewCoordinates = [containView convertPoint:CGPointZero fromView:label];;    NSLog(@"convertPointer:fromView : %@", NSStringFromCGPoint(originInContainViewCoordinates));            CGPoint originInLabelCoordinates = [containView convertPoint:CGPointZero toView:label];    NSLog(@"convertPointer:toView : %@", NSStringFromCGPoint(originInLabelCoordinates));            CGPoint originInContainViewCoordiatesConst = [containView convertPoint:CGPointMake(10, 10) fromView:label];    NSLog(@"originInContainViewCoordiatesConst : %@ ", NSStringFromCGPoint(originInContainViewCoordiatesConst));        CGPoint originInLabelCoordiatesConst = [containView convertPoint:CGPointMake(10, 10) toView:label];    NSLog(@"originInLabelCoordiatesConst : %@", NSStringFromCGPoint(originInLabelCoordiatesConst));        CGPoint originInView_LabelCoordinates = [self.view convertPoint:CGPointZero fromView:label];    NSLog(@"originInViewCoordinates : %@", NSStringFromCGPoint(originInView_LabelCoordinates));            CGPoint originInLabel_ViewCoordinates = [self.view convertPoint:CGPointZero toView:label];    NSLog(@"originInLabel_ViewCoordinates : %@", NSStringFromCGPoint(originInLabel_ViewCoordinates));            CGPoint originInView_LabelCoordinatesConst = [self.view convertPoint:CGPointMake(10, 10) toView:label];    NSLog(@"originInView_LabelCoordinatesConst : %@", NSStringFromCGPoint(originInView_LabelCoordinatesConst));

好好思考就很容易明白,上面的名字也非常好理解 : originInContainViewCoordinates  和  originInLabelCoordinates  分别是基于ContainView的坐标系 和 基于Label的坐标系

这样的输出结果就非常好理解了:抓住 from to的意义


说白了就是坐标系 和 偏移度的选取罢了。


还有一处注意的地方,你可以这样倒置,效果是一样的:

    CGPoint originInContainViewCoordinates = [containView convertPoint:CGPointZero fromView:label];    CGPoint originInContainViewCoordinates = [label convertPoint:CGPointZero toView:containView];

在头文件中我们可以见到另外两个相似的方法:

- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;


效果和用法都是一样的啦,只是改成了以CGRect作结果罢了。


参考:

http://stackoverflow.com/questions/8465659/understand-convertrecttoview-convertrectfromview-convertpointtoview-and

http://blog.csdn.net/xuhuan_wh/article/details/8486337


0 0
原创粉丝点击