`

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

 

预留

预留

预留

预留

预留

预留

分享到:
评论
1 楼 云端月影 2015-08-26  
       mark

相关推荐

    iOS布局渲染之UIView方法的调用时机详解

    APP页面优化对小编来说一直是难题,最近一直在不断的学习和总结 ,发现APP页面优化说到底离不开view的绘制和渲染机制。本文将详细给大家介绍关于iOS布局渲染之UIView方法调用时机的相关内容,下面话不多说了,来一起...

    iOS自定义控件开发梳理总结

    在日常iOS开发中,系统提供的控件常常无法满足业务功能,这个时候需要我们实现一些自定义控件。...在UIView的内部有一个CALayer,提供内容的绘制和显示,包括UIView的尺寸样式。UIView的frame实际上返回的CALayer的f

    iOS-UI控件常见属性总结

    1.UIView  // 如果userInteractionEnabled=NO,不能跟用户交互 @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // 控件的标记(父控件通过标记可以找到对应的子控件) @...

    Category:Swift Extension 扩展 分类集锦, 包含 UIView,Date,UIImage,UIColor,String,NSObject等,功能丰富

    最近开发新项目, 需要搭一个框架, 在写常用Category时候, 发现之前写的比较乱,且不够完善, 这次特意总结了下,并且丰富了category库, 框架搭好了,有时间了专门写一篇文章来总结和分享下.,如果对您有所帮助的话,麻烦给...

    ios 实用动画指南

    iOS 动画总结 UIView Animation 动画效果 及 layer 动画 非常实用

    12个iOS技术面试题及答案总结

    实际上你不是绘制一个UIView,而是子类化一个UIView并赋予绘制自己的能力。当一个UIView需要执行绘制操作时,drawRect:方法就会被调用,覆盖此方法让你获得绘图操作的机会。当drawRect:方法被调用,当前图形的上...

    iOS 开发中总结的各种工具类。

    iOS 工具类,NSString,UIImage等。###目前包括以下几类 * UIImage * UIScreen * UIView * NSString

    OBJECTIVE-C编程之道 IOS设计模式解析电子书+源代码

    模板方法18.1 何为模板方法模式18.2 何时使用模板方法18.3 利用模板方法制作三明治18.4 保证模板方法正常工作18.5 向模板方法增加额外的步骤18.6 在Cocoa Touch框架中使用模板方法18.6.1 UIView类中的定制绘图18.6.2...

    关于iOS 11的一些新特性适配实践总结

    在这里,我们做了点总结,与大家一起分享一下关于 iOS 11 一些新特性的适配。 UIView & UIViewController Layout Margins iOS 11 中,官方提供了一种新的布局方法——通过 layout margins 进行布局。官方文档 ...

    总结IOS关闭键盘/退出键盘的五种方式

    这是一种很直觉的方法,当不再需要使用虚拟键盘时,只要点击虚拟键盘和编辑区域外的地方,就可以将键盘收起,下面程式码是在 UIView 中内建的触碰事件方法函式,您可以参考 Touch Panel / 触碰萤幕 / 压力感应器的...

    WMPlayer:WMPlayer-AVPlayer的封装,继承UIView,支持pods,手势快进,快退,全面兼容全面屏,同时支持网络和本地视频的播放

    功能支持cocoapods支持旋转屏:全屏模式和小屏模式切换,自动感应旋转...支持动态改变播放器的填充模式pod使用的问题总结1,pod搜索WMPlayer如果pod搜索到WMPlayer版本为3.0.6,或者搜索不到,那么请更新Mac的ruby版

    iOS中设置view圆角化的四种方法示例

    在最近进行项目性能优化的过程中,遇到view圆角优化的问题,有一些粗略的看法,现总结一下。分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 设置圆角目前知道的有四种方法:  1、通过...

    ios-LZH_ToolView.zip

    总结了常用的几种View的使用方法、属性,大致的简单使用就这些,都是懒加载创建,使用时你只需要CV就好了,并且在设置数据时也会很方便,其中包含UIView,UIScrollview,UITableView,UICollectionView

    deepin, 在 iOS [More Deep in iOS Study ] 中,出色的教程.zip

    deepin, 在 iOS [More Deep in iOS Study ] 中,出色的教程 ##TODOiOS资深开发者必备基础技能总结参考资料十个值得思考的问题#### 用户界面基础控件:UIButton、UIImageView、UILabel、UIView等等。屏幕适配:Auto...

    iOS中的AutoLayout使用实践总结

    前言 AutoLayout非常强大也非常易用,可读性也很强,加上各种第三方AutoLayout库,让你布起局来犹如绷掉链子的狗!但在使用中也有各种各样的问题,下面就来给...一个UIView显示,需要有2个元素,position & size。Au

    JXCategoryKit:系统基础类扩展

    UIView,UITextField,UITableViewCell,UIImage,UIDevice,UIColor,UIButton,UITabBarController,UIApplication类扩展包含CoreLocation.framework内部的CLLocation类扩展包含QuartzCore.framework的CALayer类...

    iOS中如何获取某个视图的截图详析

    最近在做SDK的截图,想触发类似系统的截屏功能,找了一圈,总结一下靠谱的几种方式。 我写了个UIView 的category,将这几种方式封装和简化了一下。 第一种情形截图 这种是最最普通的截图,针对一般的视图上添加...

    iphone开发笔记

    退回输入键盘 2 CGRect 2 CGPoint & CGSize 3 设置透明度 3 设置背景色 3 ...UIView设置成圆角方法 43 iPhone里的frame和bounds区别 43 Objective-C内存管理 44 iphone更改键盘右下角按键的type 45

Global site tag (gtag.js) - Google Analytics