今天是下大雨,今天乘着下雨天来学习一下UIView的基础😆... 拒绝慌不择路的写代码
1. CGRectInset(rect,dx,dy) 平移和缩放
CGRect rect = CGRectMake(100, 100, 100 , 100);
UIView *testView = [[UIView alloc] initWithFrame:rect];
testView.backgroundColor = [UIColor redColor];
[self.view addSubview: testView];
CGRect rect1 = CGRectInset(rect, 30, 10);
NSLog(@"%@",NSStringFromCGRect(rect1));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
testView.frame = rect1;
}];
});
控制台打印: {{130, 110}, {40, 80}}
CGRectInset(rect,dx,dy)
- rect是原来的frame
- dx为正则origin x加上dx,为负数则减(PS.其实都是加,加上一个负数就是减啊)
- dy 和dx一样处理 y加上dy
- rect的size大小则是
width
是(rect.size.width - dx*2)height
是 (rect.size.height - dy*2) 就是dx越大缩放的宽度越小 dy越大缩放的高度度越小
(正缩小 负增大)
2. CGRectOffset(rect,dx,dy) 仅仅只是平移
CGRect rect = CGRectMake(100, 100, 100 , 100);
UIView *testView = [[UIView alloc] initWithFrame:rect];
testView.backgroundColor = [UIColor redColor];
[self.view addSubview: testView];
CGRect rect1 = CGRectOffset(rect, 30, 30);
NSLog(@"%@",NSStringFromCGRect(rect1));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
testView.frame = rect1;
}];
});
- 与
CGRectInset(rect,dx,dy)
同理,唯一区别就是size不变, 只有x和y的变化
3. convertRect: toView: 切换坐标系 也就是坐标变 大小不变
从一个子视图从父视图移除加到其他视图的上,需要重新计算frame,然鹅这个API就免去了去书写计算的步骤
CGRect rect = CGRectMake(100, 100, 100 , 100);
UIView *testView = [[UIView alloc] initWithFrame:rect];
testView.backgroundColor = [UIColor redColor];
[self.view addSubview: testView];
CGRect rect1 = CGRectMake(30, 30, 60 , 60);
UIView *subView = [[UIView alloc] initWithFrame:rect1];
subView.backgroundColor = [UIColor greenColor];
[testView addSubview: subView];
CGRect frame = [subView convertRect:subView.bounds toView: self.view];
NSLog(@"%@",NSStringFromCGRect(frame));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
[subView removeFromSuperview];
subView.frame = frame;
[self.view addSubview: subView];
}];
});
转换前的坐标 ==> {{30, 30}, {60, 60}}
转换后的坐标 ==> {{130, 130}, {60, 60}}
- scrollView里的子控件里的子控件如何确定在scrollView里的位置的一个方案, 还有就是tableview通过cell获取在tableview上的位置