IOS开发之UI中的坐标相关内容

本文详细介绍了iOS开发中视图的创建与变换方法,包括如何使用frame、center和bounds属性来定位视图,并展示了旋转、缩放和平移等视图变换效果。


#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [[UIViewController alloc] init];

    self.window.rootViewController.view.userInteractionEnabled = NO;

    

    //=======================================

    //创建视图对象

    UIView * view1 = [[UIView alloc] init];

    //显示在界面上

    [self.window addSubview:view1];

    //设置背景颜色

    view1.backgroundColor = [UIColor redColor];

    

    //再创建一个视图

    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    view2.backgroundColor = [UIColor orangeColor];

    [view1 addSubview:view2];

    

    

    

    //=========1.frame========

    //每个想要显示在界面上需要通过frame属性来确定坐标和大小 (默认:(0,0;0,0))

    view1.frame = CGRectMake(50, 50, 50, 50);

    

    //=========2.center=======

    //当前这个视图的中心点的坐标

    //确定了视图的frame,那么视图的center就确定

    CGPoint center = view1.center;

    NSLog(@"center:%@", NSStringFromCGPoint(center));

    

    //设置中心点 , 会改变frame的坐标

    view1.center = CGPointMake(200, 200);

    

    

    //=========3.bounds========

    //(1)视图一旦确定了frame,就确定了bounds的值;bounds坐标是(0,0),大小是framesize

    CGRect bounds = view1.bounds;

    NSLog(@"bounds:%@", NSStringFromCGRect(bounds));

    

    //(2)改变bounds的坐标,不会影响视图的frame的坐标;但是会影响当前这个视图的子视图坐标原点(一般不会去设置bounds的值,只是通过bounds快速获取坐标是(0,0)大小是当前视图大小的frame值)

    view1.bounds = CGRectMake(0, 0, 100, 100);

    

    //(3)改变boundssize会影响framesize,也会影响frame的坐标,但是视图的中心点不会变

//    view1.bounds = CGRectMake(-10, -10, 200, 200);

    NSLog(@"%@", NSStringFromCGPoint(view1.center));

    

    

    //添加一个定时器

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(ytTransform) userInfo:nil repeats:YES];

    view1.tag = 10;

    

    //===========4.形变=========

    //旋转、缩放、平移都是对视图的同一个属性进行设置,后设置的属性会覆盖之前设置的属性

    //(1)旋转(当前视图上的子视图也会跟着一起旋转)

    //参数:角度(pi对应的角度)

    //    M_PI_2  pi/2

    [view1 setTransform:CGAffineTransformMakeRotation(M_PI)];

    

    //(2)缩放(当前视图的子视图也会一起缩放)

    //参数1:x方向的缩放比例

    //参数2:y方向的缩放比例

    [view1 setTransform:CGAffineTransformMakeScale(1, 1)];

    

    

    //(3)平移

    //参数1:x方向平移单位 (为正向右移,为负向左移)

    //参数2:y方向上的平移单位 (为正向下移,为负向上移)

    [view1 setTransform:CGAffineTransformMakeTranslation(0, 0)];

    

    

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}


- (void)ytTransform{

    

    UIView * view = [_window viewWithTag:10];

    //============时时旋转===========

//    //使用静态变量存储上一次结束的时候视图的旋转角度

//    static float rotation = M_PI;

//

//    //获取需要旋转的视图


//    

//    //0.1秒让视图的旋转角度增加pi/10

//    [view setTransform:CGAffineTransformMakeRotation(rotation +  M_PI/10)];

//    

//    //保存当前视图的旋转角度

//    rotation += M_PI/10;

    

    //==========时时缩放============

//    static float scale = 1;

//    static float scaleValue = 1;

//    

//    [view setTransform:CGAffineTransformMakeScale(scale + (0.2)*scaleValue, scale + (0.2)*scaleValue)];

//    

//    //缩放后更新缩放当前的比例

//    scale += 0.2 * scaleValue;

//    

//    //当缩放比例大于4的时候让比例减小

//    if (scale >= 4) {

//        

//        scaleValue = -1;

//    }

//    

//    //当缩放比例小于0.2的时候让比例增加

//    if (scale < 0.2) {

//        

//        scaleValue = 1;

//    }

    

    //============时时平移==============

    static float translation = 0;

    static float translationValue = 1;

    

    [view setTransform:CGAffineTransformMakeTranslation(translation + 10 * translationValue, 0)];

    //更新当前平移的距离

    translation += 10 * translationValue;

    

    if (view.frame.origin.x <= 0) {

        translationValue = 1;

    }

    

    if (view.frame.origin.x >= _window.frame.size.width - view

        .frame.size.width) {

        

        translationValue = -1;

    }


    

    

    

    

    

    

    

}



- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end


转载于:https://my.oschina.net/luhoney/blog/654769

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值