今天做了一个下拉菜单的封装,封装后的控件在使用时位置的确定很是头疼,后来才想到有坐标系转化这玩意儿,这下简单多了.下面一个简单实例分享一下这个方法
UIView *grayView = [[UIViewalloc]initWithFrame:CGRectMake(100,100, 200,200)];
grayView.backgroundColor = [UIColorgrayColor];
UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(20,20, 40, 15)];
btn.backgroundColor = [UIColorredColor];
[grayView addSubview:btn];
[self.viewaddSubview:grayView];
/*
该方法的原理是:将中间参数:but.frame 从左边参数but.superview的坐标系转移到右边参数self.view的坐标系
*/
CGRect newFrame = [btn.superviewconvertRect:btn.frametoView:self.view];
//2秒后修改成转换的frame更直观
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
//动画展示
[UIViewanimateWithDuration:3animations:^{
btn.frame = newFrame;
}];
});