苹果官方文档定义连接:
bounds-UIView | Apple Developer Documentation
返回上级目录:IOS文档学习
1.定义和比较
-
bounds:在自己的坐标系中描述view的位置和大小
-
frame: 在父视图的坐标系中描述view的位置和大小
-
改变view.frame的x和y值会改变view在父视图中的位置,而改变view.bounds的x和y值会改变view的子视图在view中的位置
2.通过改变bounds的x和y来改变子视图的位置(demo(代码+gif))
2.1 如下图,绿图和蓝图是黄图的子视图,红图是绿图的子视图,初始和还原状态下所有视图的bounds的x和y值都是0,从图中我们可以观察和推导出:
- view.bounds的x值增加,所有的子视图会向左跑
- view.bounds的x值减少,所有的子视图会向右跑
- view.bounds的y值增加,所有的子视图会向上跑
- view.bounds的y值减少,所有的子视图会向下跑
2.2 上面示例的代码实现
//
// viewController.swift
// Bounds&Frame
//
// Created by macvivi on 2020/5/23.
// Copyright © 2020 macvivi. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var yellowView:UIView?
var greenView:UIView?
var redView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
setupUI();
}
func setupUI(){
yellowView = UIView.init(frame: CGRect(x: 0, y: 44, width: 350, height: 600));
yellowView?.backgroundColor = UIColor.yellow;
view.addSubview(yellowView ?? view);
greenView = UIView.init(frame: CGRect(x: 100, y: 100, width: 200, height: 250))
greenView?.backgroundColor = UIColor.green
yellowView?.addSubview(greenView ?? view);
redView = UIView.init(frame: CGRect(x: 50, y: 50, width: 100, height: 100));
redView?.backgroundColor = UIColor.red
greenView?.addSubview(redView ?? view)
let blueView = UIView.init(frame: CGRect(x: 80, y: 400, width: 100, height: 100));
blueView.backgroundColor = UIColor.blue;
yellowView?.addSubview(blueView)
let btn = UIButton.init(type: .system)
btn.frame = CGRect(x: 150, y: 400, width: 200, height: 30);
btn.setTitle("绿图bounds的y加100", for: .normal);
view.addSubview(btn);
let btn1 = UIButton.init(type: .system);
btn1.frame = CGRect(x: 150, y: 500, width: 200, height: 30);
btn1.setTitle("还原", for: .normal);
view.addSubview(btn1);
let btn2 = UIButton.init(type: .system);
btn2.frame = CGRect(x: 150, y: 600, width: 200, height: 30);
btn2.setTitle("黄图bounds的x和y都加50", for: .normal);
view.addSubview(btn2);
btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside);
btn1.addTarget(self, action: #selector(restore(btn:)), for: .touchUpInside);
btn2.addTarget(self, action: #selector(allAdd), for: .touchUpInside)
}
@objc func restore(btn:UIButton){
greenView?.bounds = CGRect(x: 0, y: 0, width: 200, height: 250);
yellowView?.bounds = CGRect(x: 0, y: 0, width: 350, height: 600);
}
@objc func btnClick(btn:UIButton){
greenView?.bounds = CGRect(x: 0, y: 100, width: 200, height: 250);
}
@objc func allAdd(){
yellowView?.bounds = CGRect(x: 50, y: 50, width: 350, height: 600);
}
}
3.bounds的使用,UIScrollView的滑动就他通过改变bounds的值来实现的
参考博客:UIScrollView详解
如下图,scrollView.bounds.origin 和 scrollView.contentOffset是等价的
演示代码:
//
// ViewController.swift
// bounds-scrollView
//
// Created by macvivi on 2020/5/25.
// Copyright © 2020 macvivi. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var scrollViewMy:UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
setupUI();
}
func setupUI(){
scrollViewMy = UIScrollView.init(frame: CGRect(x: 30,y: 50,width: 300,height: 600));
scrollViewMy.backgroundColor = UIColor.yellow;
view.addSubview(scrollViewMy);
scrollViewMy.contentSize = CGSize(width: 600, height: 1200);
// scrollViewMy.contentOffset = CGPoint(x: 300, y: 600)
scrollViewMy.delegate = self;
let bigView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: 600, height: 1200))
bigView.backgroundColor = UIColor.red
bigView.image = UIImage.init(named: "test");
scrollViewMy.addSubview(bigView);
}
}
extension ViewController:UIScrollViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("bounds")
print( scrollView.bounds.origin.x);
print( scrollView.bounds.origin.y);
print("offset")
print(scrollView.contentOffset.x)
print(scrollView.contentOffset.y)
}
}