iOS开发收集

UIView 总结

2015-11-26  本文已影响75人  方克己

博文出处:[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(π*(0~2))  //改变旋转角度
CGAffineTransform form=CGAffineTransformMakeTranslation(70, 0) //改变位置
//在0-2*PI之间比较好  旋转
CGAffineTransform form=CGAffineTransformMakeRotation(π*0.5)    //缩放
view.transform = form
view.transform = CGAffineTransformIdentity;  复原

//配合使用
//先修改 后缩放 
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 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;
swfit实现翻转

demo如右翻转:

防射变化123.gif
func initViews(){//初始化
       let view = UIView(frame:SView.bounds)
        view.backgroundColor = UIColor.redColor()
        let view1 = UIView(frame:SView.bounds)
        view1.backgroundColor = UIColor.blackColor()
        SView.addSubview(view)
        SView.addSubview(view1)
}
func animationStart(){动画执行
        UIView.beginAnimations("animationID", context: nil)
        UIView.setAnimationDuration(0.5)
        UIView.setAnimationCurve(.EaseInOut)
        UIView.setAnimationRepeatAutoreverses(false)
        UIView.setAnimationTransition(.FlipFromRight, forView:     SView, cache: true)
        SView.exchangeSubviewAtIndex(atIndex, withSubviewAtIndex: withIndex)
        UIView.commitAnimations()
}
上一篇下一篇

猜你喜欢

热点阅读