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: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];
IOS开发之UIView总结的更多相关文章
- ios开发之UIView和UIViewController
UIView 表示屏幕上的一块矩形区域,负责渲染区域的内容,并且响应该区域内发生的触摸事件.它在 iOS App 中占有绝对重要的地位,因为 iOS 中几乎所有可视化控件都是 UIView 的子类. ...
- ios开发之UIView的frame、bounds跟center属性的区别(附图)
博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...
- IOS开发之UIView的基本使用
一.视图 1. iphone手机上的窗口就是UIWindow类的一个实例(1个手机应用只有一个UIWindow). 2.UIView类用于实现视图. UIView提供了方法来添加和删除子视图.一个视图 ...
- iOS开发之UIView的常见属性
1.所有控件都继承自UIView,UIView的常见属性如下: @property(nonatomic,readonly) UIView *superview;获得自己的父控件对象 @property ...
- IOS开发之UIView总结1
太长了,请看 http://blog.csdn.net/xdrt81y/article/details/9128695 performSelector: performSelector:withObj ...
- iOS开发之UIView
在iPhone里你能看到的.摸到的,都是UIView. 视图坐标系统: UIKit中的坐标都是基于这样的坐标系统:以左上角为坐标的原点,原点向下和向右为坐标轴方向. 坐标值由浮点数来表示,内容的布局和 ...
- iOS开发之UISearchBar初探
iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...
- iOS开发之loadView、viewDidLoad及viewDidUnload的关系
iOS开发之loadView.viewDidLoad及viewDidUnload的关系 iOS开发之loadView.viewDidLoad及viewDidUnload的关系 标题中所说的3个方 ...
- IOS开发之ZBarReaderView的使用
IOS开发之ZBarReaderView的使用 HOMEABOUTGUESTBOOKCATEGORIESTAGSLINKSSUBSCRIBE 当开发IOS程序中需要用到二维码识别功能的时候,zbar这 ...
随机推荐
- iOS多线程中的单例
#import "MyHandle.h" static MyHandle *handle = nil; @implementation MyHandle // 传统写法 // 此时 ...
- 2-06. 数列求和(20)(ZJUPAT 数学)
题目链接:http://pat.zju.edu.cn/contests/ds/2-06 给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000).求数列之和S ...
- getParameter
近期学习JAVA的WEB开发,遇到Request中相关的getParameter方法问题.在网上找了一下.自己整理,以备以后查用. getParameter得到的都是String类型的.或者是用于读取 ...
- 如何安装系统认证签名过的APK
如果你的App因为权限原因需要设置 android:sharedUserId="android.uid.system" 那么IDE编译出的包通常是无法直接安装的,查看控制台会发现报 ...
- 中国A股市场缘何遭遇9连跌?
端午长假刚过,当投资者对“红六月”预期信心满满的时候,A股市场却遭遇了诡异的跌势,截止6月17日收盘,A股出现了罕见的“9连跌”.不仅上证综指失守2200点年线整数位,深证成指跌幅则高达3%以上,而且 ...
- ASP.NET事务存储过程
--修改存储过程 alter proc proc_get_student as select * from student; asp.net 的事务就是针对数据层来处理的呀! 没有数据处理不能使用事务 ...
- Android:WebView(慕课网)
使用webview最重要的三点: 1 WebView加载本地资源(webView.loadUrl("file:///android_asset/example.html");) 2 ...
- Hibernate MySQL 数据库 使用别名 报 Column * Not Found
使用Hibernate 查询MySQL数据表的时候报 Column Not Found ,原因是MySQL的驱动不支持别名, 解决方案如下,在连接参数中加上 useOldAliasMetadataBe ...
- HDU2138 随机素数测试 Miller-Rabin算法
题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In eac ...
- C# 7.0
C# 7.0 本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性 ...