如何设置UIView的原点引用? [英] How to set a UIView's origin reference?

查看:179
本文介绍了如何设置UIView的原点引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个UIImageView并将其添加到我的视图的循环中,我将初始帧设置为0,0,1,47,每次循环我更改图像视图的中心以将它们放大。



我总是使用0作为origin.y



问题是原点引用位于中心



如何更改代码中的参考点?





使用

code> UIView 您可以通过两种方式设置位置:




  • center - 它肯定是它的中心。

  • frame.origin - 左上角,


  • 如果你想让左下角在x = 300,y = 300你可以这样做:

      UIView * view = ... 
    CGRect frame = view.frame;
    frame.origin.x = 300 - frame.size.width;
    frame.origin.y = 300 - frame.size.height;
    view.frame = frame;






    但是如果你进一步深入魔法世界 CALayers (不要忘记导入QuartzCore),你就更强大了。



    CALayer 有以下这些:




    • 位置 ,它不会明确地说中心,所以它可能不是中心!

    • anchorPoint - CGPoint ,其值范围为0..1(包括),用于指定视图中的点。默认值是x = 0.5,y = 0.5,这意味着'center'(和 - [UIView center] 假设这个值)。您可以将其设置为任何其他值, position 属性将应用于该点。



    示例时间:


    1. 您的视图大小为100x100

    2. view.layer.anchorPoint = CGPointMake(1,1);

    3. view.layer.position = CGPointMake ,300);

    4. 视图的左上角是x = 200,y = 200,右下角是x = 300, 300。

    注意:旋转图层/视图时, $ c> anchorPoint ,这是默认的中心。



    Bu,因为你只是问 HOW 事情而不是您希望实现的目标,我现在无法帮助您。


    I am creating a UIImageView and adding it in a loop to my view, I set the initial frame to 0,0,1,47 and each passage of the loop I change the center of the image view to space them out.

    I am always using 0 as the origin.y

    The problem is the origin reference is in the centre of the image view, assuming we was in interface builder, this is equivalent to the image below.

    How can I change the reference point in code ?

    解决方案

    After reading these answers and your comments I'm not really sure what is your point.

    With UIView you can set position by 2 ways:

    • center – It definitely says it is the center.
    • frame.origin – Top left corner, can't be set directly.

    If you want the bottom left corner to be at x=300, y=300 you can just do this:

    UIView *view = ...
    CGRect frame = view.frame;
    frame.origin.x = 300 - frame.size.width;
    frame.origin.y = 300 - frame.size.height;
    view.frame = frame;
    


    But if you go one level deeper to magical world of CALayers (don' forget to import QuartzCore), you are more powerful.

    CALayer has these:

    • position – You see, it don't explicitely says 'center', so it may not be center!
    • anchorPointCGPoint with values in range 0..1 (including) that specifies point inside the view. Default is x=0.5, y=0.5 which means 'center' (and -[UIView center] assumes this value). You may set it to any other value and the position property will be applied to that point.

    Example time:

    1. You have a view with size 100x100
    2. view.layer.anchorPoint = CGPointMake(1, 1);
    3. view.layer.position = CGPointMake(300, 300);
    4. Top left corner of the view is at x=200, y=200 and its bottom right corner is at x=300, y=300.

    Note: When you rotate the layer/view it will be rotated around the anchorPoint, that is the center by default.

    Bu since you just ask HOW to do specific thing and not WHAT you want to achieve, I can't help you any further now.

    这篇关于如何设置UIView的原点引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆