ios开发之 -- UIView总结
如果想调用某个类的某个方法可以写成这样,这个方法来自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:];
}
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 = ;
[self.view viewWithTag:];
(UILable *)[self.view.window viewWithTag:];
视图的几何特征
//框架
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(,,,);
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(,);
中心
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(, , , );
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(,); //修改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:] setAlpha:1.0f];
//标志动画块结束
[UIView commitAnimations];
//还可以设置回调
[UIView setAnimationDelegate:self];
//设置回调调用的方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
视图翻转
UIView *whiteBackdrop = [self.view viewWithTag:];
// 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:]];
NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:]];
//交换视图
[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 :
animation.type = kCATransitionFade;
break;
case :
animation.type = kCATransitionMoveIn;
break;
case :
animation.type = kCATransitionPush;
break;
case :
animation.type = kCATransitionReveal;
default:
break;
}
//设置渐变的方向,上下左右
if (isLeft)
animation.subtype = kCATransitionFromRight;
else
animation.subtype = kCATransitionFromLeft; // Perform the animation
UIView *whitebg = [self.view viewWithTag:];
NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:]];
NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:]];
[whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white];
[[whitebg layer] addAnimation:animation forKey:@"animation"];
animation.type还可以用以下的赋值
switch (theButton.tag) {
case :
animation.type = @"cube";
break;
case :
animation.type = @"suckEffect";
break;
case :
animation.type = @"oglFlip";
break;
case :
animation.type = @"rippleEffect";
break;
case :
animation.type = @"pageCurl";
break;
case :
animation.type = @"pageUnCurl";
break;
case :
animation.type = @"cameraIrisHollowOpen ";
break;
case :
animation.type = @"cameraIrisHollowClose ";
break;
default:
break;
}
休眠一下
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];
一个简单的通过图片做的动画
// Load butterfly images
NSMutableArray *bflies = [NSMutableArray array];
for (int i = ; i <= ; 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 = ;
//设置动画的图片
butterflyView.animationImages = bflies;
//设置时间
butterflyView.animationDuration = 0.75f;
[self.view addSubview:butterflyView];
//开始动画
[butterflyView startAnimating];
[butterflyView release];
ios开发之 -- UIView总结的更多相关文章
- IOS开发-UIView之动画效果的实现方法(合集)
http://www.cnblogs.com/GarveyCalvin/p/4193963.html 前言:在开发APP中,我们会经常使用到动画效果.使用动画可以让我们的APP更酷更炫,最重要的是优化 ...
- 【iOS开发】UIView之userInteractionEnabled属性介绍
http://my.oschina.net/hmj/blog/108002 属性作用 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互. 当值设置为NO后,UI ...
- 【iOS开发】---- UIView动画
iOS 动画UIView动画 原文:http://www.cocoachina.com/bbs/read.php?tid=110168 1.概述 UIKit直接将动画集成到UIView类中,实现简 ...
- iOS开发实现UIView随着子控件的高度的变化而变化
例子 其实看完上面的叙述,你会思考,到底什么情况下,一个UIView需要只设置坐标不设置大小呢?其实这种场景相当普遍.比如,我们常常会碰到,一个View中有两个Label,两个Label的高度均和内容 ...
- iOS开发UI篇—核心动画(UIView封装动画)
iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏
1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...
- iOS开发之指定UIView的某几个角(小于4)为圆角
在iOS开发中,我们经常会遇到View设置圆角的问题,如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore ...
- iOS 开发之动画篇 - 从 UIView 动画说起
毋庸置疑的:在iOS开发中,制作动画效果是最让开发者享受的环节之一.一个设计严谨.精细的动画效果能给用户耳目一新的效果,吸引他们的眼光 —— 这对于app而言是非常重要的. 本文作为动画文集的第一篇, ...
随机推荐
- CDH quick start VM 中运行wordcount例子
需要注意的事情: 1. 对于wordcount1.0 ,按照http://www.cloudera.com/content/cloudera/en/documentation/HadoopTutori ...
- spring Log4j关于No appenders could be found for logger的警告
(spring环境下)配置Log4j时候,当启动WEB程序时,提示了如标题的警告,具体如下:log4j:WARN No appenders could be found for logger (org ...
- 视频中的DTS与PTS的理解
原文地址:http://blog.itpub.net/30004768/viewspace-1338882/ DTS(解码时间戳)和PTS(显示时间戳)分别是解码器进行解码和显示帧时相对于SCR(系 ...
- C#日期格式化英文月份 VS改大小写的快捷键
DateTime.Now.ToString("MMM yyyyy",CultureInfo.CreateSpecificCulture("en-GB")) ; ...
- mysql 用户自定义变量
SQL中可以用变量来操作值.那么问题就来了.mysql中怎么定义一个变量呢? 一.定义变量 1.定义变量的语法: set @var_name=expr [,@var_name=expr] ... 2. ...
- 揭开Altera公司支持OpenCL的设计工具的神秘面纱
将程序中处理负荷较大的工作分配给加速器LSI的“异构计算(Heterogeneous Computing)”将踏出崭新的一步.美国Altera公司将于2013年内开始面向普通用户提供可自动由按照异构计 ...
- 这不是bug,而是语言特性
分析编程语言缺陷的一种方法是把所有的缺陷归于3类:不该做的做了,该做的没做,该做但做得不合适. 在使用switch case时,如果使用缺省的 fall through,请一定在旁边注释,因为97%的 ...
- dp之多重背包poj2392
题意:有k种石头,高为hi,在不超过ai的高度下,这种石头可以放置,有ci种这个石头,求这些石头所能放置的最高高度......... 思路:以往的什么硬币种数,最大硬币数之类的,他们的硬币都已经是排好 ...
- imx6 socketcan 发送问题
参考cansend 的方法进行发送can 数据. cansend 的生成,请查考:http://www.cnblogs.com/chenfulin5/p/6797756.html cansend 代码 ...
- hdu6121 Build a tree 模拟
/** 题目:hdu6121 Build a tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:n个点标号为0~n-1:节点i的父节点 ...