码迷,mamicode.com
首页 > 移动开发 > 详细

IOS之UIKit_Day20

时间:2015-03-08 22:56:33      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

Day20

回顾:AutoLAyout的代码实现:

          方法一:7分参数的完整公式法(每次创建一个约束)

          方法二:VFL可视化的字符串(每次可以创建多个约束)

          注意事项:

                   a.创建的约束要添加到父视图中

                   b.关闭视图及父是视图的自动编译 Autoresizing为约束的这个对象

2 动画

          1>UIImage

          2>NSTimer实现动画

                   匀速动画:当前值=开始值+当前帧数*(结束值-开始值)/(帧率*动画时长)

                   变速动画:当前值=上一次的值+(目标值-上一次的值)*渐进因子

          3>使用UIView实现动画的思路

          a.要改变的数据有一个初始状态

          b.添加动画

          c.在Block块中确定动画结束时,希望数据变化的结果

         

今天:

1. Core Animation

          1.1 是什么?

          是一个图形渲染和动画的底层框架,用于IOS和MAC OS X

          1.2 能干什么?

          1》可以提供更多强大的图形渲染(显示)效果

          2》可以提供专业级别的动画

          3》是高级图形技术的基础

eg

对根视图进行渲染       

CALayer *layer=self.view.layout;

layer.backgroundColor=[[UIColor grayColor]CGColor]

设置边角弧度

layer.CornerRadius=30.0

设置边缘遮罩

layer.masksToBounds=YES;

          .shadowColor//设置阴影的颜色

          .shadowOffset//设置阴影的大小

          .shadowRadius//设置边角弧度

          .shadowOpacity//设置透明 默认是0不透明

imageLayer.contents=(Id)[UIImage imageName:@"001.jpg"].CGImage//设置要添加子视图的内容

 

          1.3 如何使用Core Animation

          (内容多  很庞大  只讲常用以后自己学习总结)

          通过CALayer;类 直接对一个视图(UIView及子类)的Core Animation层进行一设置,达到需要的渲染效果。

          1.4 如何获得CALayer这一层?

          任何UIView及其子类都有一个属性叫:LAyer

          UIView                           CALayer

          .Layer                                              

          .frame                            .frame

          .transform                      .transform3D

          .Autoresizing                  .Autoresizing

          .addSubView                  .addSubLayer

          1.5CAAnimation          

          一个抽象的动画类型,很多时候不关心这个父类,而是使用它的子类实现动画

          1》CAKeyFrameAnimation关键帧动画

                   可以根据指定的路径进行动画

                   step1:创建关键帧动画

                   使用AnimationWithKeyPath:方法创建 同时最后一个字符串的参数必须是一下类型

          position//位移    transform//变形   opacity//设置透明

                   step2:设置动画属性

                   step3:将动画添加到视图的Layer层上

          2》CABasicAnimation可以实现缩放 旋转 透明度 等动画

                   特点:设置动画属性主要分为两个:fromValue  和toValue

//创建关键帧动画

    CAKeyframeAnimation*moveAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];

    //设置属性

    moveAnimation.path=path.CGPath; //路径

    moveAnimation.duration=2.0;//持续时间

    [self.imageView.layeraddAnimation:moveAnimation forKey:nil];

 

//创建缩放动画

    CABasicAnimation*scaleAnimation=[CABasicAnimation animationWithKeyPath:@"transform"];

    //设置开始的大小

    scaleAnimation.fromValue=[NSValue valueWithCATransform3D:CATransform3DIdentity];

    //设置结束的大小

    scaleAnimation.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 0.1)];

    scaleAnimation.duration=2.0;

    [self.imageView.layeraddAnimation:scaleAnimation forKey:nil];

 

//创建透明度动画

    CABasicAnimation*alphaAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];

    alphaAnimation.fromValue=@0.1;

    alphaAnimation.toValue=@0.0;

    alphaAnimation.duration=2.0;

    [self.imageView.layeraddAnimation:alphaAnimation forKey:nil];

          3CAAnimationGroup创建动画的时候将所有的动画添加到组中,针对组的动画属性会被用到组的每一动画中

          CAAnimationGroup*group=[CAAnimationGroup animation];

          group.animation=@[moveAnimation,scaleAnimation,alphaAnimation];

          group.duration.2.0;

          group.delegate=self;

          [self.imageView.layeraddAnimation:group forKey:nil];

实现方法:当结束时,图片停留子啊动画结束的位置

          - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag{

    [self.imageView removeFromSuperview];

}

1.6 CATransform3D

          1》是什么?

          是一个4*4的矩阵,一个结构体描述了一个3D图片变形的数据

          2》创建

          CATransform3DMakeRotation/scale/translation

          CATransforma3DScale/Rotate/Translate  在传入值的基础上进行变形

 

2. UIKit Dynamic动力特效

          2.1 是什么?

          中文翻译:UIKit动力  动力模型

          2.2 UIKit Dynamic 结构

          a. 核心部分:UIDynamicAnimator---》视图的坐标系

          b. UIDynamic XX Behavior(行为)

                   重力UIGravityBehavior

                   碰撞UICollisionBehavior

                   吸附

                   闪烁

                   推理

                   综合力

          重力(gravity

          准备:定义两个全局的UIDynamicAnimationUIGravityBehavior

                   1》创建UIDynamic的实例

                   UIDynamicAnimation*animator=[[UIDynamicAnimation alloc]initWithReferenceViewLayout:self.view];

                   self.animation=animation;

                   2>创建UIGravityBehavior的实例并设置要给谁添加重力特效

                            UIGravityBehavior*gravity=[[UICravityBehavior alloc]initWithItem:@[self.image]];

3. 设置重力行为的强度

                   gravity.magnitude=1   决定了下降的速度

          4. 把重力行为添加到 UIDynamic的实例中

                   [animatoraddBehavior:gravity];

 

          碰撞:(collisionBehavior

          1 创建UICollisionBehavior的实例

                   IUCollisionBehavior*collision=[[UICollisionBehavior alloc]initWithItems:@[self.imageView]];

          2.设置场景视图的四周为可碰撞的四条边

                   collision.translatesReferenceBoundsIntoBoundary=YES;

          3.把碰撞行为添动力特效中

                   [animationaddBehavior:collision];

*******可以在中重力行为开始倒碰撞行为完成之前添加一个障碍物

          1)在碰撞行为2之后添加一条障碍物

                   UIBezierPath *path=[UIBezierPathbezierPathWithRoundedRect:CGRectMake:(20,360,280,30) cornerRadius:10.0]设置障碍物的位置以及障碍物边角的弧度

          2)创建一个新的BackgroundView继承子UIView  公开一个bezierPath

                   重写//绘制传入的障碍物图形

          - (void)drawRect:(CGRect)rect

          {

          [[UIColor redColor]setFill];

          [[UIColor greenColor]setStroke];

        [self.path stroke];

         [self.path fill];

          }

          3)强转self.view

                   backgroundView  *myView=backgroundView *self.View

                   myView.path=path;

                   [myView  setNeedsDisplay];

          4)把障碍物添加到碰撞行为中

                   [collisionaddBoundaryWithIdentifier:@MyPath1forPath:path];

          5)为碰撞时可以设置物体的属性比如颜色的改变等等

          collision.collisionDelegate=self;因为该方法是早期的 所以实现代理需要实现

          -(void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:  (id<UIDynamicItem>)itemwithBoundaryIdentifier:(id<NSCopying>)identifier atPoint:   (CGPoint)p{

    //NSLog(@"...");

    UIImageView *box = (UIImageView *)item;

    box.tintColor = [UIColor redColor];

    box.image = [box.imageimageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

          }

 

          吸附:(attachment

          1 定义全局变量 animation  gravity attachment

          2. 初始化三个全局属性

          - (UIDynamicAnimator *)animator{

          if (!_animator) {

               _animator = [[UIDynamicAnimatoralloc]initWithReferenceView:self.view];

       }

        return _animator;

          }

 

          - (UIGravityBehavior *)gravity{

        if (!_gravity) {

            _gravity = [[UIGravityBehavioralloc]initWithItems:@[self.imageView]];

        }

        return _gravity;

          }

 

          - (UIAttachmentBehavior *)attachment{

            CGPoint attachmentAnchor =CGPointMake(self.imageView.center.x, self.imageView.center.y - 100);

          if (!_attachment) {

           _attachment = [[UIAttachmentBehavioralloc]initWithItem:self.imageView attachedToAnchor:attachmentAnchor];

          }

        return _attachment;

          }

 

          3. self.View上添加手势并实现一下方法

          -(IBAction)tap:(UIPanGestureRecognizer *)sender {

        CGPoint location = [senderlocationInView:self.view];

          //将手势滑动到的点作为吸附行为的锚点

        self.attachment.anchorPoint = location;

          }

                  

          4. viewWillAppear方法中实现

                   [self.animatoraddBehavior:self.gravity];

                   self.attachment.action=^{

                   //绘制锚点倒中心点的悬挂线

                   UIBezierPath        *path=[UIBezierPath bezierPath];

                   [pathmoveToPoint:self.attachment.anchorPoint];

                   [pathaddLineToPoint:self.imageView.center];

                   backgroundView  *bgView=(backgroundView *)self.view;

                   bgView.path=path;

                   path.lineWidth=5;

                   [bgView setNeedsDisplay];

                   };

                    [self.animatoraddBehavior:self.attachment]                 

                  

IOS之UIKit_Day20

标签:

原文地址:http://www.cnblogs.com/katydid/p/4322311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!