转自:http://langhua9527.iteye.com/blog/1377741

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

  1. performSelector:
  2. performSelector:withObject:
  3. performSelector:withObject:withObject:

实际调用

  1. [self performSelector:@selector(displayViews) withObject:nil afterDelay:1.0f];

有三个方法分别是

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

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

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

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

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

找出所有的父视图

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

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

  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:

视图回调

  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设置标记和检索视图

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

视图的几何特征

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

仿射变换

  1. CGAffineTransform form = CGAffineTransformMakeRotation(PI);
  2. myview.transform = form;

如想复原

  1. myview.transform = CGAffineTransformIdentity;

直接设置视图的中心

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

中心

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

定时器

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

定义视图边界

  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

  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. //修改CGAffineTransform
  8. //修改 缩放
  9. CGAffineTransform scaled = CGAffineTransformScale(transform, degree, degree);
  10. //修改 位置
  11. CGAffineTransform transform = CGAffineTransformTranslate(
  12. CGAffineTransform t,
  13. CGFloat tx,
  14. CGFloat ty
  15. );
  16. //修改角度
  17. CGAffineTransform transform = CGAffineTransformRotate (
  18. CGAffineTransform t,
  19. CGFloat angle
  20. );
  21. //最后设置到VIEW
  22. [self.view setTransform:scaled];

建立UIView动画块

//首先建立CGContextRef

  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:)];

视图翻转

  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. }
  8. NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]];
  9. NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]];
  10. //交换视图
  11. [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];
  12. //还有上翻和下翻两种 如下
  13. typedef enum {
  14. //没有任何效果
  15. UIViewAnimationTransitionNone,
  16. UIViewAnimationTransitionFlipFromLeft,
  17. UIViewAnimationTransitionFlipFromRight,
  18. UIViewAnimationTransitionCurlUp,
  19. UIViewAnimationTransitionCurlDown,
  20. } UIViewAnimationTransition;

使用QuartzCore做动画

  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. // Perform the animation
  31. UIView *whitebg = [self.view viewWithTag:10];
  32. NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:99]];
  33. NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:100]];
  34. [whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white];
  35. [[whitebg layer] addAnimation:animation forKey:@"animation"];

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

  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

休眠一下

  1. [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];

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

  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. }
  6. UIImageView *butterflyView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0f, 300.0f, 60.0f, 60.0f)];
  7. butterflyView.tag = 300;
  8. //设置动画的图片
  9. butterflyView.animationImages = bflies;
  10. //设置时间
  11. butterflyView.animationDuration = 0.75f;
  12. [self.view addSubview:butterflyView];
  13. //开始动画
  14. [butterflyView startAnimating];
  15. [butterflyView release];

UIView总结的更多相关文章

  1. UIView的layoutSubviews和drawRect方法何时调用

    首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...

  2. iOS 自定义方法 - UIView扩展

    示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...

  3. UIView上的控件使用push方法跳转

    有时候在项目中,为了保持前后页面的推进方式跳转方式一致,会在通过UIview上的控件跳到另一个Controller上,所以,这时候就需要用到这种方式了,当然,present方法可以实现跳转但是样式可能 ...

  4. IOS 杂笔-11(实现在外部无法改变UIView的size)

    我想题目说的或许不是很清楚,那么现在我详细介绍一下这篇随笔内容. 在外部无法改变UIVIew控件的size. 这里说是UIView,但是事实上,是大多数控件而绝非仅UIView. 想要实现在外部无法改 ...

  5. iOS系列 基础篇 05 视图鼻祖 - UIView

    iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...

  6. 5. UIView

    1. UIView 的初认识 官方文档 UIView class defines a rectangular area on the screen and the interfaces for man ...

  7. Swift - UIView,UItableView,Cell设置边框方法

    // 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...

  8. iOS 使点击事件穿透透明的UIView

    如图: 悬浮的三个按钮下方有一个可以点击的灰色区域,但是点击按钮之间的透明区域, 这三个按钮的contentView会响应这个点击事件,这时候需要让这个contentView不响应这个点击事件. 解决 ...

  9. iOS----自定义UIView,绘制一个UIView

    绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时,drawRect:方 ...

  10. UIView的几个layout方法

    iOS layout的相关方法: 1,layoutSubviews 2,layoutIfNeeded 3,setNeedsLayout 4,setNeedsDisplay 5,drawRect 6,s ...

随机推荐

  1. 配置Android-Annotation (github20大开源:http://www.eoeandroid.com/thread-278980-1-1.html)

    1. 把androidannotations-X.X.X-api.jar 放在libs文件夹 2. 把androidannotations-X.X.X.jar 放在文件夹compile-libs,1与 ...

  2. 有用的iOS网站地址

    王巍 (@onevcat) 是一名 iOS 和 Unity3D 开发者,现旅居日本,正在寻求创意之源.http://swifter.tips/http://onevcat.com/2013/02/xc ...

  3. 给文件夹添加Everyone用户

    DOC命令 C# code 1. cacls C:dming /g everyone:f /e /t 这样可以添加 2. cacls C:Program Files客友软件 /g everyone:f ...

  4. HDU 2897 邂逅明下 (博弈)

    题意: 给你n.p.q,每次操作是令n减小 [p, q]区间中的数,当n < p时必须全部取完了,取完最后一次的人算输,问先手必胜还是必败. 解题思路: 这种非常类似巴什博弈,可以找出必胜区间和 ...

  5. Java中this与super

    l  对象的this引用 作用: this关键字就是让类中一个方法,访问该类中的另一个方法或属性. 1.构造器中引用该构造器正在初始化的对象. 2.在方法中引用调用该方法的对象(哪个对象调用的方法,t ...

  6. 裸裸的线段树(hdu 1754)

    线段树的第一发. 哪天忘了还能够让自己找找回顾. 线段树操作: build  : 建树. update:点改动: query:查询 Input 在每一个測试的第一行,有两个正整数 N 和 M ( 0& ...

  7. js 字符串indexof与search方法的区别

    1.indexof方法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法: 注意:有可选的参数(即设置开始的检索位置). 2.search方法 search() 方法用 ...

  8. 〖Linux〗不重复启动某应用程序的脚本

    cmd="/home/scue/bin/ipclient $ipclient" exist=$(ps aux | grep -v 'grep' | grep "$cmd& ...

  9. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  10. DB2删除重复数据

    有时候DB2建表时不设置主键,就可能存在脏数据,例如:两条一样数据重复存在,这时候就需要将重复记录删除,然后留下一条记录. )); ----插入重复数据 ,'jack'); 插入数据后,结果如下图: ...