UIView常用属性、方法和动画

来源:互联网 发布:mac ppt 页面设置 编辑:程序博客网 时间:2024/05/06 19:48


UIView的显示位置:


将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法。

将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法。

[self.view insertSubview:girlView belowSubview:bottomView];//把girlView插入到bottomView后面

 [self.view insertSubview:girlView aboveSubview:bottomView];//把girlView插入到bottomView前面

 [self.view insertSubview:girlView atIndex:0];//把girlView插入到0层


圆角
       //设置圆角边框
        myview.layer.cornerRadius = 8;
        myview.layer.masksToBounds = YES;
        //设置边框及边框颜色
        myview.layer.borderWidth = 8;
        myview.layer.borderColor =[ [UIColor grayColor] CGColor];

局部圆角
 UIBezierPath*maskPath = [UIBezierPathbezierPathWithRoundedRect:self.submitAlertView.boundsbyRoundingCorners:UIRectCornerTopLeft| UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
   
CAShapeLayer*maskLayer = [[CAShapeLayeralloc]init];
    maskLayer.
frame= self.submitAlertView.bounds;
    maskLayer.
path= maskPath.CGPath;
    self.submitAlertView.layer.mask= maskLayer;


  1. //父视图   
  2. [self.view superview]  
  3. //所有子视图  
  4.  [self.view subviews]  
  5. //自身的window  
  6.  self.view.window  
  1. //加一个视图到一个视图里面  
  2. addSubview:  
  3. //将一个视图移到前面  
  4. bringSubviewToFront:  
  5. //将一个视图推送到背后  
  6. sendSubviewToBack:  
  7. //把视图移除  
  8. removeFromSuperview  
  9. //插入视图 并指定索引  
  10. insertSubview:atIndex:  
  11. //插入视图在某个视图之上  
  12. insertSubview:aboveSubview:  
  13. //插入视图在某个视图之下  
  14. insertSubview:belowSubview:  
  15. //交换两个位置索引的视图  
  16. exchangeSubviewAtIndex:withSubviewAtIndex:  

视图回调

 

  1. //当加入视图完成后调用  
  2. (void)didAddSubview:(UIView *)subview  
  3. //当视图移动完成后调用  
  4. (void)didMoveToSuperview  
  5. //当视图移动到新的WINDOW后调用  
  6. (void)didMoveToWindow  
  7. //在删除视图之后调用  
  8. (void)willRemoveSubview:(UIView *)subview  
  9. //当移动视图之前调用  
  10. (void)didMoveToSuperview:(UIView *)subview  
  11. //当视图移动到WINDOW之前调用  
  12. (void)didMoveToWindow  

建立UIView动画块

   //首先建立CGContextRef

  1. CGContextRef context = UIGraphicsGetCurrentContext();  
  2. //标记动画开始  
  3. [UIView beginAnimations:nil context:context];  
  4. //定义动画加速或减速的方式  
  5. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  6. //定义动画的时长 1秒  
  7. [UIView setAnimationDuration:1.0];  
  8. //中间处理 位置变化,大小变化,旋转,等等的  
  9. [[self.view viewWithTag:999] setAlpha:1.0f];  
  10. //标志动画块结束  
  11. [UIView commitAnimations];  
  12. //还可以设置回调  
  13. [UIView setAnimationDelegate:self];  
  14. //设置回调调用的方法  
  15. [UIView setAnimationDidStopSelector:@selector(animationFinished:)];  

  视图翻转

 

  1. UIView *whiteBackdrop = [self.view viewWithTag:100];  
  2. // Choose left or right flip 选择左或右翻转  
  3. if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]){  
  4. [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:whiteBackdrop cache:YES];  
  5. }else{  
  6. [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:whiteBackdrop cache:YES];  
  7. }  
  8.     NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]];  
  9.     NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]];  
  10. //交换视图  
  11.     [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];  
  12.   
  13. //还有上翻和下翻两种 如下  
  14. typedef enum {  
  15. //没有任何效果  
  16.     UIViewAnimationTransitionNone,  
  17.     UIViewAnimationTransitionFlipFromLeft,  
  18.     UIViewAnimationTransitionFlipFromRight,  
  19.     UIViewAnimationTransitionCurlUp,  
  20.     UIViewAnimationTransitionCurlDown,  
  21. } UIViewAnimationTransition;  

  使用QuartzCore做动画

 

  1. //创建CATransition  
  2. CATransition *animation = [CATransition animation];  
  3. //设置代理  
  4. animation.delegate = self;  
  5. //设置动画过渡的时间  
  6. animation.duration = 4.0f;  
  7. //定义动画加速或减速的方式   
  8. animation.timingFunction = UIViewAnimationCurveEaseInOut;  
  9. //animation.type 表示设置过渡的种类 如 Fade,MoveIn,Push,Reveal  
  10. switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) {  
  11.         case 0:  
  12.             animation.type = kCATransitionFade;  
  13.             break;  
  14.         case 1:  
  15.             animation.type = kCATransitionMoveIn;  
  16.             break;  
  17.         case 2:  
  18.             animation.type = kCATransitionPush;  
  19.             break;  
  20.         case 3:  
  21.             animation.type = kCATransitionReveal;  
  22.         default:  
  23.             break;  
  24.     }  
  25. //设置渐变的方向,上下左右  
  26.     if (isLeft)  
  27.         animation.subtype = kCATransitionFromRight;  
  28.     else  
  29.         animation.subtype = kCATransitionFromLeft;  
  30.   
  31. // Perform the animation  
  32.     UIView *whitebg = [self.view viewWithTag:10];  
  33.     NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:99]];  
  34.     NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:100]];  
  35.     [whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white];  
  36.     [[whitebg layer] addAnimation:animation forKey:@"animation"];  

 

休眠一下

 

  1. [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];  

UIView 3D 旋转

//围绕y轴旋转PI,即镜面效果

view.layer.transform = CATransform3DConcat(view.layer.transformCATransform3DMakeRotation(M_PI,0.0,1.0,0.0));

 

一些例子:

//使view围绕view的左下角这个点旋转90度

    UIView *view = [self.view viewWithTag:100];//获取一个view

    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI*0.5);//逆时针为负数,正则是顺时针

    

    CGRect frame = view.frame;

    view.layer.anchorPoint =CGPointMake(0,1);//设置旋转的中心点(锚点)

    view.frame = frame;//设置anchorPont会使view的frame改变。重新赋值。

    

  //一个2秒的动画,实现旋转。

    [UIView beginAnimations:nilcontext:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    [UIView setAnimationDuration:2.0f];

    view.transform = transform;

    [UIView commitAnimations]; 

 

 *****************

view.transform = CGAffineTransformMakeScale(0.5,0.5);//缩放50%

view.transform = CGAffineTransformIdentity;//还原

 

翻书效果:

1、

                CGRect frame = view.frame;

                view.layer.anchorPoint = CGPointMake(1.0f0.5f);

                view.frame = frame;

                [UIView beginAnimations:nilcontext:nil];

                [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

                [UIView setAnimationDuration:2.0f];

                view.layer.transform = CATransform3DMakeRotation(M_PI*0.50.01.00);

                [UIView commitAnimations];  

 

2、

                CGRect frame = view.frame;

                view.layer.anchorPoint = CGPointMake(1.0f0.5f);

                view.frame = frame;

                view.layer.position = CGPointMake(view.layer.position.x + view.bounds.size.width/2.0f, view.layer.position.y);

                CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform"];

                animation.duration = 2.0f;

                animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

                CATransform3D tfm = CATransform3DMakeRotation(M_PI/2.0f0.0f1.0f0.0f);

                tfm.m34 = 0.001f;

                tfm.m14 = -0.0015f;

                animation.fromValue = [NSValuevalueWithCATransform3D:CATransform3DIdentity];

                animation.toValue = [NSValue valueWithCATransform3D:tfm];

                [view.layer addAnimation:animation forKey:@"flipUp"];

两种效果一样。




0 0
原创粉丝点击