frame.bounds和center

本文深入探讨了iOS开发中UIKit框架的视图坐标系统,包括frame、bounds和center属性的概念与应用。通过实例讲解了如何使用CGRect、CGPoint和CGSize描述视图的位置和大小,并解释了这些属性之间的区别和联系。

CGPoint point=CGPoint(x,y);  //表示位置

CGSize size=CGSzieMake(width,height);  //表示大小

CGRect rect=CGRectMake(x,y,width,height)

 

1.frame:

描述当前视图在其父视图中的位置和大小,用位置坐标和长度来表示:

sample:

 

UIButton *button3=[[[UIButtonalloc]initWithFrame:CGRectMake(120,120,100,100)]autorelease];

    button3.backgroundColor=[UIColorgreenColor];

    [self.view addSubview:button3];

    NSLog(@"the result is %f,%f,%f,%f",button3.frame.size.height,button3.frame.size.width,button3.frame.origin.x,button3.frame.origin.y);

结果:

 

the result is 100.000000,100.000000,120.000000,120.000000

2. bounds  property

描述当前视图在其自身坐标系统中的位置和大小。

iphone中坐标系统的建立,最左上角是原点(0,0),向右为x轴递增,想下为y轴递减。

 

ios采用CGPoint来表示点在坐标系上X、Y位置。我们可以通过CGPointMake(x,y)来创建一个坐标点:CGPoint point = CGPointMake(80,40)

同时,ios采用CGSize来表示视图的宽度和高度,即视图的大小。我们可以通过CGSizeMake(width,height)来创建一个矩形的大小,如CGSize size = CGSizeMake(144,72)将创建一个宽度为144,高度为72的矩形大小。

而CGRect则是结合了CGPoint和CGSize,用来表示矩形的位置和大小。它的origin表示矩形右上角所在位置(CGPoint),size表示矩形的大小(CGSize)。

sample:

 

 

  UIButton *button3=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 100)] autorelease];
    button3.backgroundColor=[UIColor greenColor];
    [self.view addSubview:button3];
    NSLog(@"the result is %f,%f,%f,%f",button3.frame.size.height,button3.frame.size.width,button3.frame.origin.x,button3.frame.origin.y);
    
    NSLog(@"the result is %f,%f,%f,%f",button3.bounds.origin.x,button3.bounds.origin.y,button3.bounds.size.height,button3.bounds.size.width);
}

 

 

3.center  property

描述当前视图的中心点在其父视图中的位置。

sample如下所示:

    UIButton *button3=[[[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 100)] autorelease];
    button3.backgroundColor=[UIColor greenColor];
    [self.view addSubview:button3];
    NSLog(@"the result is %f,%f",button3.center.x,button3.center.y);

result is:

 

 

the result is 170.000000,170.000000

4.frame.bounds 和center的区别和联系

这两个属性都是用来描述视图的大小(CGSize)和位置(CGPoint)的,两者都用CGRect表示。不同的是,frame描述的是在其父视图中的CGRect,而bounds描述的是在其自身视图中的CGRect,

center属性则用CGPoint表示矩形中心点在其父视图中的位置,frame、bounds和center三个属性是相互关联、相互影响的,其中一个属性发生变化,其他属性也会跟着变化。


using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { private Rigidbody2D rb; private BoxCollider2D coll; private SpriteRenderer sprite; private Animator anim; [SerializeField] private LayerMask jumpableGround; private float dirX = 0f; [SerializeField] private float moveSpeed = 7f; [SerializeField] private float jumpForce = 7f; private enum MovementState { idle, running, jumping, falling } // Start is called before the first frame update private void Start() { rb = GetComponent<Rigidbody2D>(); coll = GetComponent<BoxCollider2D>(); sprite = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); } } // Update is called once per frame private void Update() { dirX = Input.GetAxisRaw("Horizontal"); rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y); if (Input.GetButtonDown("Jump") && IsGrounded()) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } UpdateAnimationState(); } private void UpdateAnimationState() { MovementState state; if (dirX > 0f) { state = MovementState.running; sprite.flipX = false; } else if (dirX < 0f) { state = MovementState.running; sprite.flipX = true; } else { state = MovementState.idle; } if (rb.velocity.y > .1f) { state = MovementState.jumping; } else if (rb.velocity.y < -.1f) { state = MovementState.falling; } anim.SetInteger("state", (int)state); } private bool IsGrounded() { return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround); } }
最新发布
06-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值