UIView相关方法

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移到最后

如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类

C代码  收藏代码
  1. performSelector: 
  2. performSelector:withObject: 
  3. performSelector:withObject:withObject: 

实际调用

C代码  收藏代码
  1. [self performSelector:@selector(displayViews) withObject:nil afterDelay:1.0f]; 

  有三个方法分别是

C代码  收藏代码
  1. //父视图  
  2. [self.view superview] 
  3. //所有子视图 
  4. [self.view subviews] 
  5. //自身的window 
  6. self.view.window 

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

C代码  收藏代码
  1. NSArray *allSubviews(UIView *aView) 
  2.     NSArray *results = [aView subviews]; 
  3.     for (UIView *eachView in [aView subviews]) 
  4.     { 
  5.         NSArray *riz = allSubviews(eachView); 
  6.         if (riz) { 
  7.             results = [results arrayByAddingObjectsFromArray:riz]; 
  8.         } 
  9.     } 
  10.     return results; 

循环返回一个APPLICATION里面所有的VIEW

C代码  收藏代码
  1. // Return all views throughout the application 
  2. NSArray *allApplicationViews() 
  3.     NSArray *results = [[UIApplication sharedApplication] windows]; 
  4.     for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
  5.     { 
  6.         NSArray *riz = allSubviews(window); 
  7.         if (riz) results = [results arrayByAddingObjectsFromArray: riz]; 
  8.     } 
  9.     return results; 

找出所有的父视图

C代码  收藏代码
  1. // Return an array of parent views from the window down to the view 
  2. NSArray *pathToView(UIView *aView) 
  3.     NSMutableArray *array = [NSMutableArray arrayWithObject:aView]; 
  4.     UIView *view = aView; 
  5.     UIWindow *window = aView.window; 
  6.     while (view != window) 
  7.     { 
  8.         view = [view superview]; 
  9.         [array insertObject:view atIndex:0]; 
  10.     } 
  11.     return array; 

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

C代码  收藏代码
  1. //加一个视图到一个视图里面 
  2. addSubview: 
  3. //将一个视图移到前面 
  4. bringSubviewToFront: 
  5. //将一个视图推送到背后 
  6. sendSubviewToBack: 
  7. //把视图移除 
  8. removeFromSuperview 
  9. //插入视图 并指定索引 
  10. insertSubview:atIndex: 
  11. //插入视图在某个视图之上 
  12. insertSubview:aboveSubview: 
  13. //插入视图在某个视图之下 
  14. insertSubview:belowSubview: 
  15. //交换两个位置索引的视图 
  16. exchangeSubviewAtIndex:withSubviewAtIndex: 

视图回调

C代码  收藏代码
  1. //当加入视图完成后调用 
  2. (void)didAddSubview:(UIView *)subview 
  3. //当视图移动完成后调用 
  4. (void)didMoveToSuperview 
  5. //当视图移动到新的WINDOW后调用 
  6. (void)didMoveToWindow 
  7. //在删除视图之后调用 
  8. (void)willRemoveSubview:(UIView *)subview 
  9. //当移动视图之前调用 
  10. (void)didMoveToSuperview:(UIView *)subview 
  11. //当视图移动到WINDOW之前调用 
  12. (void)didMoveToWindow 

给UIView设置标记和检索视图

C代码  收藏代码
  1. myview.tag = 1001; 
  2. [self.view viewWithTag:1001]; 
  3. (UILable *)[self.view.window viewWithTag:1001]; 

视图的几何特征

C代码  收藏代码
  1. //框架 
  2. struct CGPoint { 
  3.   CGFloat x; 
  4.   CGFloat y; 
  5. }; 
  6. typedef struct CGPoint CGPoint; 
  7.  
  8. /* Sizes. */ 
  9.  
  10. struct CGSize { 
  11.   CGFloat width; 
  12.   CGFloat height; 
  13. }; 
  14. typedef struct CGSize CGSize; 
  15.  
  16. struct CGRect { 
  17.   CGPoint origin; 
  18.   CGSize size; 
  19. }; 
  20. typedef struct CGRect CGRect; 
  21.  
  22.  
  23.  
  24. CGRect rect = CGRectMake(0,0,320,480); 
  25. UIView *view = [[UIView allow]initWithFrame:rect]; 
  26.  
  27. //将String转成CGPoint 如 @”{3.0,2.5}”    {x,y} 
  28. CGPoint CGPointFromString ( 
  29.    NSString *string 
  30. ); 
  31.  
  32. //将String转成CGRect  @”{{3,2},{4,5}}”  {{x,y},{w, h}} 
  33. CGRect CGRectFromString ( 
  34.    NSString *string 
  35. ); 
  36.  
  37. //将String转成CGSize @”{3.0,2.5}” {w, h} 
  38. CGSize CGSizeFromString ( 
  39.    NSString *string 
  40. ); 
  41.  
  42. //CGPoint转成NSString 
  43. NSString * NSStringFromCGPoint ( 
  44.    CGPoint point 
  45. ); 
  46.  
  47. //CGRect转成NSString 
  48. NSString * NSStringFromCGRect ( 
  49.    CGRect rect 
  50. ); 
  51.  
  52. //CGSize转成NSString 
  53. NSString * NSStringFromCGSize ( 
  54.    CGSize size 
  55. ); 
  56.  
  57. //对一个CGRect进行修改 以这个的中心来修改 正数表示更小(缩小) 负数表示更大(放大) 
  58. CGRect CGRectInset ( 
  59.    CGRect rect, 
  60.    CGFloat dx, 
  61.    CGFloat dy 
  62. ); 
  63.  
  64. //判断两个矩形是否相交 
  65. bool CGRectIntersectsRect ( 
  66.    CGRect rect1, 
  67.    CGRect rect2 
  68. ); 
  69.  
  70. //初始为0的 
  71. const CGPoint CGPointZero; 
  72. const CGRect CGRectZero; 
  73. const CGSize CGSizeZero; 
  74.  
  75. //创建CGPoint 
  76. CGPoint CGPointMake ( 
  77.    CGFloat x, 
  78.    CGFloat y 
  79. ); 
  80. //创建CGRect 
  81. CGRect CGRectMake ( 
  82.    CGFloat x, 
  83.    CGFloat y, 
  84.    CGFloat width, 
  85.    CGFloat height 
  86. ); 
  87. //创建CGSize 
  88. CGSize CGSizeMake ( 
  89.    CGFloat width, 
  90.    CGFloat height 
  91. ); 

仿射变换

C代码  收藏代码
  1. CGAffineTransform form = CGAffineTransformMakeRotation(PI); 
  2. myview.transform = form; 

如想复原

C代码  收藏代码
  1. myview.transform = CGAffineTransformIdentity; 

直接设置视图的中心

C代码  收藏代码
  1. myview.center = CGPointMake(100,200); 

中心

C代码  收藏代码
  1. CGRectGetMinX 
  2. CGRectGetMinY 
  3. //X的中间值 
  4. CGRectGetMidX 
  5. //Y的中间值 
  6. CGRectGetMidY 
  7. CGRectGetMaxX 
  8. CGRectGetMaxY 

  定时器

C代码  收藏代码
  1. NSTime *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(move:) userInfo:nil repeats:YES]; 

  定义视图边界

C代码  收藏代码
  1. typedef struct UIEdgeInsets { 
  2.     CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset' 
  3. } UIEdgeInsets; 
  4. //eg 
  5. UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5); 
  6. CGRect innerRect = UIEdgeInsetsInsetRect([aView bounds], insets); 
  7. CGRect subRect = CGRectInset(innerRect, self.frame.size.width / 2.0f, self.frame.size.height / 2.0f); 

仿射变换补充

//创建CGAffineTransform

C代码  收藏代码
  1. //angle 在0-2*PI之间比较好  旋转 
  2. CGAffineTransform transform = CGAffineTransformMakeRotation(angle); 
  3. //缩放  
  4. CGAffineTransform transform = CGAffineTransformMakeScale(0.5f,0.5f); 
  5. //改变位置的 
  6. CGAffineTransform transform = CGAffineTransformMakeTranslation(50,60); 
  7.  
  8. //修改CGAffineTransform 
  9. //修改 缩放  
  10. CGAffineTransform scaled = CGAffineTransformScale(transform, degree, degree); 
  11. //修改 位置 
  12. CGAffineTransform transform = CGAffineTransformTranslate( 
  13.    CGAffineTransform t, 
  14.    CGFloat tx, 
  15.    CGFloat ty 
  16. ); 
  17.  
  18. //修改角度  
  19. CGAffineTransform transform = CGAffineTransformRotate ( 
  20.    CGAffineTransform t, 
  21.    CGFloat angle 
  22. ); 
  23. //最后设置到VIEW 
  24. [self.view setTransform:scaled]; 

建立UIView动画块

   //首先建立CGContextRef

C代码  收藏代码
  1. CGContextRef context = UIGraphicsGetCurrentContext(); 
  2. //标记动画开始 
  3. [UIView beginAnimations:nil context:context]; 
  4. //定义动画加速或减速的方式 
  5. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
  6. //定义动画的时长 1秒 
  7. [UIView setAnimationDuration:1.0]; 
  8. //中间处理 位置变化,大小变化,旋转,等等的 
  9. [[self.view viewWithTag:999] setAlpha:1.0f]; 
  10. //标志动画块结束 
  11. [UIView commitAnimations]; 
  12. //还可以设置回调 
  13. [UIView setAnimationDelegate:self]; 
  14. //设置回调调用的方法 
  15. [UIView setAnimationDidStopSelector:@selector(animationFinished:)]; 

  视图翻转

C代码  收藏代码
  1. UIView *whiteBackdrop = [self.view viewWithTag:100]; 
  2. // Choose left or right flip 选择左或右翻转 
  3. if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]){ 
  4. [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:whiteBackdrop cache:YES]; 
  5. }else{ 
  6. [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:whiteBackdrop cache:YES]; 
  7.     NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]]; 
  8.     NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]]; 
  9. //交换视图 
  10.     [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon]; 
  11.  
  12. //还有上翻和下翻两种 如下 
  13. typedef enum { 
  14. //没有任何效果 
  15.     UIViewAnimationTransitionNone, 
  16.     UIViewAnimationTransitionFlipFromLeft, 
  17.     UIViewAnimationTransitionFlipFromRight, 
  18.     UIViewAnimationTransitionCurlUp, 
  19.     UIViewAnimationTransitionCurlDown, 
  20. } UIViewAnimationTransition; 

  使用QuartzCore做动画

C代码  收藏代码
  1. //创建CATransition 
  2. CATransition *animation = [CATransition animation]; 
  3. //设置代理 
  4. animation.delegate = self; 
  5. //设置动画过渡的时间 
  6. animation.duration = 4.0f; 
  7. //定义动画加速或减速的方式  
  8. animation.timingFunction = UIViewAnimationCurveEaseInOut; 
  9. //animation.type 表示设置过渡的种类 如 Fade,MoveIn,Push,Reveal 
  10. switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) { 
  11.         case 0: 
  12.             animation.type = kCATransitionFade; 
  13.             break; 
  14.         case 1: 
  15.             animation.type = kCATransitionMoveIn; 
  16.             break; 
  17.         case 2: 
  18.             animation.type = kCATransitionPush; 
  19.             break; 
  20.         case 3: 
  21.             animation.type = kCATransitionReveal; 
  22.         default: 
  23.             break; 
  24.     } 
  25. //设置渐变的方向,上下左右 
  26.     if (isLeft) 
  27.         animation.subtype = kCATransitionFromRight; 
  28.     else 
  29.         animation.subtype = kCATransitionFromLeft; 
  30.  
  31. // Perform the animation 
  32.     UIView *whitebg = [self.view viewWithTag:10]; 
  33.     NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:99]]; 
  34.     NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:100]]; 
  35.     [whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white]; 
  36.     [[whitebg layer] addAnimation:animation forKey:@"animation"]; 

animation.type还可以用以下的赋值

C代码  收藏代码
  1. switch (theButton.tag) {   
  2.         case 0:   
  3.             animation.type = @"cube";   
  4.             break;   
  5.         case 1:   
  6.             animation.type = @"suckEffect";   
  7.             break;   
  8.         case 2:   
  9.             animation.type = @"oglFlip";   
  10.             break;   
  11.         case 3:   
  12.             animation.type = @"rippleEffect";   
  13.             break;   
  14.         case 4:   
  15.             animation.type = @"pageCurl";   
  16.             break;   
  17.         case 5:   
  18.             animation.type = @"pageUnCurl";   
  19.             break;   
  20.         case 6:   
  21.             animation.type = @"cameraIrisHollowOpen ";   
  22.             break;   
  23.         case 7:   
  24.             animation.type = @"cameraIrisHollowClose ";   
  25.             break;   
  26.         default:   
  27.             break;   
  28.     }   

  上面这个是转自这里的http://2015.iteye.com/blog/1122130

休眠一下

C代码  收藏代码
  1. [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]]; 

一个简单的通过图片做的动画

C代码  收藏代码
  1. // Load butterfly images 
  2. NSMutableArray *bflies = [NSMutableArray array]; 
  3. for (int i = 1; i <= 17; i++){ 
  4.     [bflies addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"bf_%d", i] ofType:@"png"]]]; 
  5. UIImageView *butterflyView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0f, 300.0f, 60.0f, 60.0f)]; 
  6. butterflyView.tag = 300; 
  7.        //设置动画的图片 
  8. butterflyView.animationImages = bflies; 
  9.        //设置时间 
  10. butterflyView.animationDuration = 0.75f; 
  11. [self.view addSubview:butterflyView]; 
  12.        //开始动画 
  13. [butterflyView startAnimating]; 
  14. [butterflyView release]; 

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会使view的frame改变。重新赋值。

   

  //一个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"];

两种效果一样。

动画的各种几何特性,大都可以通过此方法设定:

  1. [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"]; 

[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];

                  

  从官方的API上的解释,可以看出 hitTest方法中,要先调用PointInside:withEvent:,看是否要遍历子视图。如果我们不想让某个视图响应事件,只需要重载PointInside:withEvent:方法,让此方法返回NO就行了。

转载于:https://my.oschina.net/zhangzhihao/blog/101480

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值