UIView Api

来源:互联网 发布:大学生网络课程app 编辑:程序博客网 时间:2024/06/03 07:04

  新一轮的UI开始了,课程很繁忙,遵老师命令写一篇博客,详细介绍一下UIView.


//    UIView   API

    

    //继承关系...UIResponder : NSObject

    

    //框架..UIKit in iOS 2.0 and later

    

    

    //UIView类定义了一个矩形区域在屏幕上和管理内容的接口。在运行时,一个视图对象处理任何内容的呈现它的面积,还处理任何与内容交互。UIView类本身提供了基本行为与背景颜色填充的矩形区域。UIKit框架还包含一组标准的子类,范围从简单的按钮到复杂的表,可以按原样使用。例如,UILabel对象画了一个文本字符串和UIImageView对象吸引一个图像

    

    

    

    //因为视图对象是主要的应用程序与用户交互的方式,他们有许多责任。这里有几个

    //.绘图和动画

    //    .视图绘制内容在矩形区域使用UIKit等技术,核心图形,OpenGL ES

    //    .一些可以动画视图属性的新值

    

    //.布局和子视图管理

    //    .一个视图可以包含零个或多个中的

    //    .每个视图定义了自己的默认的调整行为与它的父视图

    

    

    //视图可以嵌入其他视图,创建复杂的视觉层次。这将创建一个父子关系视图被嵌入(称为子视图)和嵌入的父视图(称为superview)。通常,子视图的可见区域的范围不剪superview,但在iOS可以使用clipsToBounds财产行为的改变。父视图可以包含任意数量的中的只有一个superview,但每个子视图负责定位中的适当。

    

    

    

    //层级关系

    //[self.view insertSubview:girlView belowSubview:bottomView];//girlView插入到bottomView后面

    

    //[self.view insertSubview:girlView aboveSubview:bottomView];//girlView插入到bottomView前面

    

    //[self.view insertSubview:girlView atIndex:0];//girlView插入到0

    

    //[self.view bringSubviewToFront:girlView];//girlView移到最前

    

    //[self.view sendSubviewToBack:girlView];//girlView移到最后

    


    

    //循环一个视图下面所有视图的方法

    

//    NSArray *allSubviews(UIView *aView)

//    {

//        NSArray *results = [aView subviews];

//        for (UIView *eachView in [aView subviews])

//        {

//            NSArray *riz = allSubviews(eachView);

//            if (riz) {

//                results = [results arrayByAddingObjectsFromArray:riz];

//            }

//        }

//        return results;  

//    }

    

    

    

    

//    UIView提供了大量管理视图的方法

    

    

//    //加一个视图到一个视图里面

//addSubview:

//    //将一个视图移到前面

//bringSubviewToFront:

//    //将一个视图推送到背后

//sendSubviewToBack:

//    //把视图移除

//    removeFromSuperview

//    //插入视图 并指定索引

//insertSubview:atIndex:

//    //插入视图在某个视图之上

//insertSubview:aboveSubview:

//    //插入视图在某个视图之下

//insertSubview:belowSubview:

//    //交换两个位置索引的视图

//exchangeSubviewAtIndex:withSubviewAtIndex:

    

    

    

//    视图回调

    

    

//    //当加入视图完成后调用

//    (void)didAddSubview:(UIView *)subview

//    //当视图移动完成后调用

//    (void)didMoveToSuperview

//    //当视图移动到新的WINDOW后调用

//    (void)didMoveToWindow

//    //在删除视图之后调用

//    (void)willRemoveSubview:(UIView *)subview

//    //当移动视图之前调用

//    (void)didMoveToSuperview:(UIView *)subview

//    //当视图移动到WINDOW之前调用

//    (void)didMoveToWindow

    

    

    

//    UIView设置标记和检索视图

//    

//    

//    

//    myview.tag = 1001;

//    [self.view viewWithTag:1001];

//    (UILable *)[self.view.window viewWithTag:1001];

    

    

    

    

    

//    视图的几何特征

//    

//    

//    

//    //框架

//    struct CGPoint {

//        CGFloat x;

//        CGFloat y;

//    };

//    typedef struct CGPoint CGPoint;

//    

//    /* Sizes. */

//    

//    struct CGSize {

//        CGFloat width;

//        CGFloat height;

//    };

//    typedef struct CGSize CGSize;

//    

//    struct CGRect {

//        CGPoint origin;

//        CGSize size;

//    };

//    typedef struct CGRect CGRect;

//    

//    

//    

//    CGRect rect = CGRectMake(0,0,320,480);

//    UIView *view = [[UIView allow]initWithFrame:rect];

//    

//    //String转成CGPoint @”{3.0,2.5}”    {x,y}

//    CGPoint CGPointFromString (

//                               NSString *string

//                               );

//    

//    //String转成CGRect  @”{{3,2},{4,5}}”  {{x,y},{w, h}}

//    CGRect CGRectFromString (

//                             NSString *string

//                             );

//    

//    //String转成CGSize @”{3.0,2.5}” {w, h}

//    CGSize CGSizeFromString (

//                             NSString *string

//                             );

//    

//    //CGPoint转成NSString

//    NSString * NSStringFromCGPoint (

//                                    CGPoint point

//                                    );

//    

//    //CGRect转成NSString

//    NSString * NSStringFromCGRect (

//                                   CGRect rect

//                                   );

//    

//    //CGSize转成NSString

//    NSString * NSStringFromCGSize (

//                                   CGSize size

//                                   );

//    

//    //对一个CGRect进行修改以这个的中心来修改 正数表示更小(缩小)负数表示更大(放大)

//    CGRect CGRectInset (  

//                        CGRect rect,  

//                        CGFloat dx,  

//                        CGFloat dy  

//                        );  

//    

//    //判断两个矩形是否相交  

//    bool CGRectIntersectsRect (  

//                               CGRect rect1,  

//                               CGRect rect2  

//                               );  

//    

//    //初始为0  

//    const CGPoint CGPointZero;  

//    const CGRect CGRectZero;  

//    const CGSize CGSizeZero;  

//    

//    //创建CGPoint  

//    CGPoint CGPointMake (  

//                         CGFloat x,  

//                         CGFloat y  

//                         );  

//    //创建CGRect  

//    CGRect CGRectMake (  

//                       CGFloat x,  

//                       CGFloat y,  

//                       CGFloat width,  

//                       CGFloat height  

//                       );  

//    //创建CGSize  

//    CGSize CGSizeMake (  

//                       CGFloat width,  

//                       CGFloat height  

//                       );  


    

    

//    

//    直接设置视图的中心

//    

//    

//    

//    myview.center = CGPointMake(100,200);

    

    

//    中心

//    

//    

//    

//    CGRectGetMinX

//    CGRectGetMinY

//    //X的中间值

//    CGRectGetMidX

//    //Y的中间值

//    CGRectGetMidY  

//    CGRectGetMaxX  

//    CGRectGetMaxY

    

    

    

    //定时器

    

    

    

//    NSTime *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(move:) userInfo:nil repeats:YES];

    

    

    

    //仿射变换补充

    

    //创建CGAffineTransform

    

    //angle 0-2*PI之间比较好  旋转

//    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);

//    //缩放

//    CGAffineTransform transform = CGAffineTransformMakeScale(0.5f,0.5f);

//    //改变位置的

//    CGAffineTransform transform = CGAffineTransformMakeTranslation(50,60);

//    

//    //修改CGAffineTransform

//    //修改 缩放

//    CGAffineTransform scaled = CGAffineTransformScale(transform, degree, degree);

//    //修改 位置

//    CGAffineTransform transform = CGAffineTransformTranslate(

//                                                             CGAffineTransform t,

//                                                             CGFloat tx,

//                                                             CGFloat ty

//                                                             );

//    

//    //修改角度

//    CGAffineTransform transform = CGAffineTransformRotate (

//                                                           CGAffineTransform t,

//                                                           CGFloat angle

//                                                           );

//    //最后设置到VIEW

//    [self.view setTransform:scaled];

    

    

    

    

    

//    建立UIView动画块

//    

//    //首先建立CGContextRef

//    

//    CGContextRef context = UIGraphicsGetCurrentContext();

//    //标记动画开始

//    [UIView beginAnimations:nil context:context];

//    //定义动画加速或减速的方式

//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

//    //定义动画的时长 1

//    [UIView setAnimationDuration:1.0];

//    //中间处理位置变化,大小变化,旋转,等等的

//    [[self.view viewWithTag:999] setAlpha:1.0f];

//    //标志动画块结束

//    [UIView commitAnimations];

//    //还可以设置回调

//    [UIView setAnimationDelegate:self];

//    //设置回调调用的方法

//    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];

//    视图翻转

//    

//    

//    

//    UIView *whiteBackdrop = [self.view viewWithTag:100];

//    // Choose left or right flip 选择左或右翻转

//    if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]){

//        [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:whiteBackdrop cache:YES];

//    }else{

//        [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:whiteBackdrop cache:YES];

//    }

//    NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]];

//    NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]];

//    //交换视图

//    [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];

//    

//    //还有上翻和下翻两种如下

//    typedef enum {

//        //没有任何效果

//        UIViewAnimationTransitionNone,

//        UIViewAnimationTransitionFlipFromLeft,

//        UIViewAnimationTransitionFlipFromRight,

//        UIViewAnimationTransitionCurlUp,

//        UIViewAnimationTransitionCurlDown,

//    } UIViewAnimationTransition;

//    使用QuartzCore做动画

//    

//    

//    

//    //创建CATransition

//    CATransition *animation = [CATransition animation];

//    //设置代理

//    animation.delegate = self;

//    //设置动画过渡的时间

//    animation.duration = 4.0f;

//    //定义动画加速或减速的方式

//    animation.timingFunction = UIViewAnimationCurveEaseInOut;

//    //animation.type 表示设置过渡的种类 Fade,MoveIn,Push,Reveal

//    switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) {

//        case 0:

//            animation.type = kCATransitionFade;

//            break;

//        case 1:

//            animation.type = kCATransitionMoveIn;

//            break;

//        case 2:

//            animation.type = kCATransitionPush;

//            break;

//        case 3:

//            animation.type = kCATransitionReveal;

//        default:

//            break;

//    }

//    //设置渐变的方向,上下左右

//    if (isLeft)

//        animation.subtype = kCATransitionFromRight;

//    else

//        animation.subtype = kCATransitionFromLeft;

//    

//    // Perform the animation

//    UIView *whitebg = [self.view viewWithTag:10];

//    NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:99]];

//    NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:100]];  

//    [whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white];  

//    [[whitebg layer] addAnimation:animation forKey:@"animation"];  


    

    

    

    

//    UIView 3D 旋转

//    

//    //围绕y轴旋转PI,即镜面效果

//    

//    view.layer.transform = CATransform3DConcat(view.layer.transform, CATransform3DMakeRotation(M_PI,0.0,1.0,0.0));

//    

//    

//    

//    一些例子:

//    

//    //使view围绕view的左下角这个点旋转90

//    

//    UIView *view = [self.view viewWithTag:100];//获取一个view

//    

//    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI*0.5);//逆时针为负数,正则是顺时针

//    

//    

//    

//    CGRect frame = view.frame;

//    

//    view.layer.anchorPoint =CGPointMake(0,1);//设置旋转的中心点(锚点)

//    

//    view.frame = frame;//设置anchorPont会使viewframe改变。重新赋值。

//    

//    

//    

//      //一个2秒的动画,实现旋转。

//    

//    [UIView beginAnimations:nilcontext:nil];

//    

//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

//    

//    [UIView setAnimationDuration:2.0f];

//    

//    view.transform = transform;

//    

//    [UIView commitAnimations];

//    

//    

//    

//    *****************

//    

//    view.transform = CGAffineTransformMakeScale(0.5,0.5);//缩放50%

//    

//    view.transform = CGAffineTransformIdentity;//还原

//    

//    

//    

//    翻书效果:

//    

//    1

//    

//    CGRect frame = view.frame;

//    

//    view.layer.anchorPoint = CGPointMake(1.0f, 0.5f);

//    

//    view.frame = frame;

//    

//    [UIView beginAnimations:nilcontext:nil];

//    

//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

//    

//    [UIView setAnimationDuration:2.0f];

//    

//    view.layer.transform = CATransform3DMakeRotation(M_PI*0.5, 0.0, 1.0, 0);

//    

//    [UIView commitAnimations];

//    

//    

//    

//    2

//    

//    CGRect frame = view.frame;

//    

//    view.layer.anchorPoint = CGPointMake(1.0f, 0.5f);

//    

//    view.frame = frame;

//    

//    view.layer.position = CGPointMake(view.layer.position.x + view.bounds.size.width/2.0f, view.layer.position.y);

//    

//    CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform"];

//    

//    animation.duration = 2.0f;

//    

//    animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

//    

//    CATransform3D tfm = CATransform3DMakeRotation(M_PI/2.0f, 0.0f, 1.0f, 0.0f);

//    

//    tfm.m34 = 0.001f;

//    

//    tfm.m14 = -0.0015f;

//    

//    animation.fromValue = [NSValuevalueWithCATransform3D:CATransform3DIdentity];

//    

//    animation.toValue = [NSValue valueWithCATransform3D:tfm];

//    

//    [view.layer addAnimation:animation forKey:@"flipUp"];

    



0 0
原创粉丝点击