iOS移动开发iOS基础软件工程

UIView与UIWindow,UIRespnder那些事儿

2015-09-04  本文已影响2584人  Tuberose

UIView与UIWindow

UIWindow* aWindow = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

CALayer

UIWindow

1.创建一个全频的window
self.window = [[UIWindow alloc] initWithFram:[UIScreen mainScreen].bounds];
2.在window中放入根控制器
self.window.rootViewControl = rootViewControl;
3.将window设置为keyWindow并显示window
[self.window makeKeyAndVisible];
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
iphone屏幕分辨率:
iphone4前的设备:320*480
iphone4和4s:640*960
iphone5和5s:640*1136
iphone6:750*1334
iphone6p:1242*2208
ipad   ipad2:1024*768
ipad3和4  ipad4:2048*1536
ipad mini:1024*768

UIView

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
  myView.backgroundColor=[UIColor redColor];
  [self.view addSubview:myView];
}
CGPoint point = CGPointMake(x,y);//左上角坐标的位置
CGSize size = CGSizeMake(width,height);//大小
CGRect rect = CGRectMake(x,y,width,height);//位置和大小  ```

- UIView的常用属性:

```objc
frame: 相对父视图的位置和大小
bounds:相对自身的位置和大小,所以bounds的x和y永远为0
center:子视图的中点坐标相对父视图的位置
transform:可以通过这个属性控制视图的放大缩小和旋转
superview:获取父视图
subviews:获取所有子视图
alpha:视图的透明度(0-1)
tag:视图的标志,设置了tag后,可以通过viewWithTag方法拿到这个视图
userInteractionEnabled:是否响应用户事件
CGAffineTransform transform = rootView.transform;
rootVIew.transform = CGAffineTransformMakeScale(0.5, 0.5);//缩放
rootView.transform = CGAffineTransformScale(transform,0.5,0.5)//在原来的基础上再缩放
rootView.transform = CGAffineTransformMakeRotation(M_2_PI);//旋转传入的角度是弧度制的
rootView.transform = CGAffineTransformRotate(transform,M_PI_4);
rootView.transform = CGAffineTransformMakeTranslation(100, 100);//平移
rootView.transform = CGAffineTransformTranslate(transform, 100, 100);
- (void)removeFromSuperview;将视图从父视图中移除
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;插入一个视图到指定位置,视图越在下面,index越小
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;将index1和index2位置的两个视图互换位置

- (void)addSubview:(UIView *)view;添加视图到父视图
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;插入视图到指定视图的下面
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;插入视图到指定视图上面

- (void)bringSubviewToFront:(UIView *)view;把视图移到最顶层
- (void)sendSubviewToBack:(UIView *)view;把视图移到最底层

- (UIView *)viewWithTag:(NSInteger)tag;     根据视图的tag属性找到搜索视图
UIVIew中支持动画的属性有以下几个:
frame:可以通过frame来改变视图的尺寸和位置
bounds:可以通过bounds来改变视图的尺寸
center:可以通过center来改变视图的位置
transform:可以通过transform来使视图翻转和缩放,平移
alpha:可以通过alpha修改视图的透明度
backgroundColor:改变视图的背景颜色
contentStetch:改变视图内容如何拉伸
1.
    [UIView beginAnimations:nil context:nil];//开始动画
    [UIView setAnimationDuration:2];//持续时间
    //动画
    CGAffineTransform transform = sender.transform;
    sender.transform = CGAffineTransformMakeTranslation(100, 100);
    sender.transform = CGAffineTransformTranslate(transform, 100, 100);

    [UIView commitAnimations];//提交动画
    2.使用block
    [UIView animateWithDuration:2 animations:^{
        CGAffineTransform transform = sender.transform;
        sender.transform = CGAffineTransformMakeTranslation(100, 100);
        sender.transform = CGAffineTransformTranslate(transform, 100, 100);
    }];
    3.使用block
    [UIView animateWithDuration:2 animations:^{
        CGAffineTransform transform = sender.transform;
        sender.transform = CGAffineTransformMakeTranslation(100, 100);
        sender.transform = CGAffineTransformTranslate(transform, 100, 100);
    } completion:^(BOOL finished) {
        //动画完成后调用的代码段
    }];
动画常用设置
[UIView setAnimationDuration:2];动画持续时间
[UIView setAnimationRepeatCount:1];动画重复次数

UIRespnder

上一篇下一篇

猜你喜欢

热点阅读