UIView总结
转自:http://langhua9527.iteye.com/blog/1377741
如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类
- performSelector:
- performSelector:withObject:
- performSelector:withObject:withObject:
实际调用
- [self performSelector:@selector(displayViews) withObject:nil afterDelay:1.0f];
有三个方法分别是
- //父视图
- [self.view superview]
- //所有子视图
- [self.view subviews]
- //自身的window
- self.view.window
循环一个视图下面所有视图的方法
- 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;
- }
循环返回一个APPLICATION里面所有的VIEW
- // Return all views throughout the application
- NSArray *allApplicationViews()
- {
- NSArray *results = [[UIApplication sharedApplication] windows];
- for (UIWindow *window in [[UIApplication sharedApplication] windows])
- {
- NSArray *riz = allSubviews(window);
- if (riz) results = [results arrayByAddingObjectsFromArray: riz];
- }
- return results;
- }
找出所有的父视图
- // Return an array of parent views from the window down to the view
- NSArray *pathToView(UIView *aView)
- {
- NSMutableArray *array = [NSMutableArray arrayWithObject:aView];
- UIView *view = aView;
- UIWindow *window = aView.window;
- while (view != window)
- {
- view = [view superview];
- [array insertObject:view atIndex:0];
- }
- return array;
- }
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
- );
仿射变换
- CGAffineTransform form = CGAffineTransformMakeRotation(PI);
- myview.transform = form;
如想复原
- myview.transform = CGAffineTransformIdentity;
直接设置视图的中心
- 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];
定义视图边界
- typedef struct UIEdgeInsets {
- CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
- } UIEdgeInsets;
- //eg
- UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5);
- CGRect innerRect = UIEdgeInsetsInsetRect([aView bounds], insets);
- CGRect subRect = CGRectInset(innerRect, self.frame.size.width / 2.0f, self.frame.size.height / 2.0f);
仿射变换补充
//创建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"];
animation.type还可以用以下的赋值
- switch (theButton.tag) {
- case 0:
- animation.type = @"cube";
- break;
- case 1:
- animation.type = @"suckEffect";
- break;
- case 2:
- animation.type = @"oglFlip";
- break;
- case 3:
- animation.type = @"rippleEffect";
- break;
- case 4:
- animation.type = @"pageCurl";
- break;
- case 5:
- animation.type = @"pageUnCurl";
- break;
- case 6:
- animation.type = @"cameraIrisHollowOpen ";
- break;
- case 7:
- animation.type = @"cameraIrisHollowClose ";
- break;
- default:
- break;
- }
上面这个是转自这里的http://2015.iteye.com/blog/1122130
休眠一下
- [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];
一个简单的通过图片做的动画
- // Load butterfly images
- NSMutableArray *bflies = [NSMutableArray array];
- for (int i = 1; i <= 17; i++){
- [bflies addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"bf_%d", i] ofType:@"png"]]];
- }
- UIImageView *butterflyView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0f, 300.0f, 60.0f, 60.0f)];
- butterflyView.tag = 300;
- //设置动画的图片
- butterflyView.animationImages = bflies;
- //设置时间
- butterflyView.animationDuration = 0.75f;
- [self.view addSubview:butterflyView];
- //开始动画
- [butterflyView startAnimating];
- [butterflyView release];
UIView总结的更多相关文章
- UIView的layoutSubviews和drawRect方法何时调用
首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...
- iOS 自定义方法 - UIView扩展
示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...
- UIView上的控件使用push方法跳转
有时候在项目中,为了保持前后页面的推进方式跳转方式一致,会在通过UIview上的控件跳到另一个Controller上,所以,这时候就需要用到这种方式了,当然,present方法可以实现跳转但是样式可能 ...
- IOS 杂笔-11(实现在外部无法改变UIView的size)
我想题目说的或许不是很清楚,那么现在我详细介绍一下这篇随笔内容. 在外部无法改变UIVIew控件的size. 这里说是UIView,但是事实上,是大多数控件而绝非仅UIView. 想要实现在外部无法改 ...
- iOS系列 基础篇 05 视图鼻祖 - UIView
iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...
- 5. UIView
1. UIView 的初认识 官方文档 UIView class defines a rectangular area on the screen and the interfaces for man ...
- Swift - UIView,UItableView,Cell设置边框方法
// 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...
- iOS 使点击事件穿透透明的UIView
如图: 悬浮的三个按钮下方有一个可以点击的灰色区域,但是点击按钮之间的透明区域, 这三个按钮的contentView会响应这个点击事件,这时候需要让这个contentView不响应这个点击事件. 解决 ...
- iOS----自定义UIView,绘制一个UIView
绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时,drawRect:方 ...
- UIView的几个layout方法
iOS layout的相关方法: 1,layoutSubviews 2,layoutIfNeeded 3,setNeedsLayout 4,setNeedsDisplay 5,drawRect 6,s ...
随机推荐
- WPF性能调试系列 – 内存监测
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants M ...
- mysql中函数greatest 与MAX区别
greatest (a,b,c,d,d)max(a) 这样就能看明白了,greatest 求的是某几列的最大值,横向求最大(一行记录)max(a) 一看就明白了,是给纵向求最大(多行记录).
- LaTeX如何设置段落层次结构
Latex的文档层次结构大约有5层,分别是: section — subsection — subsubsection — paragraph — subparagraph 具体使用可以参考下面的例子 ...
- Guava API - FluentIterable Predicate Function Odering Range Splitter
这写API可解决的问题 1. 集合元素的过滤 - FluentIterable Predicate Range Function 1) 先说Predicate<T>,这个相当与一个过滤原则 ...
- SQL用例集锦
SQL 语句分类 DDL - 数据定义语句 (CREATE,ALTER,DROP,DECLARE) DML - 数据操纵语句 (SELECT,DELETE,UPDATE,INSERT) DCL - 数 ...
- go语言基础之复数类型
1.复数类型 示例1: package main //必须有一个main包 import "fmt" func main() { var t complex128 //声明 t = ...
- python2 python3编码问题记录
最近在服务器上跑脚本,linux自带的是python 2.x,中文显示经常有问题,通过下面两篇终于弄懂了. https://www.cnblogs.com/575dsj/p/7112767.html ...
- 【crontab】“bad minute”及“errors in crontab file, can't install”错误处理
今天有朋友提到,在使用crontab定制后台定时备份任务时报出“bad minute”及“errors in crontab file, can't install”错误.经确认,根本原因是cront ...
- RS中的关系引用不明确以及行列自动添加了追溯
问题1:由于在FM模型的物理层是自己新建了查询主题,查询主题采用对已经存在的表进行sql处理然后重命名, 使用的是sql类型的数据源,sql类型设置的查询类型是默认的cognos类型,而语法是本地的结 ...
- JS实现的MAP结构数据
Array.prototype.remove = function(s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) ...