最近研究了一下,Swift语言中关于Animation动画的实现学习,分两次进行相关内容的讲解

用表格列出各种动画情况

Demo首页显示展示了一种动画显示方式,代码如下:

//绘画装饰
    func drawDecorate(){
        //画出小圆
        let smallCenterPoint = CGPointMake(50, 50);
        let smallRadiusFloat:CGFloat = sqrt(800);
        let smallMaskPath = UIBezierPath.init(ovalInRect: CGRectInset(CGRectMake(smallCenterPoint.x, smallCenterPoint.y, 1, 1), -smallRadiusFloat, -smallRadiusFloat));
        
        //画出大圆
        let largeCenterPoint = CGPointMake(50, 50);
        let largeRadiusFloat = sqrt(pow(view.bounds.width-largeCenterPoint.x, 2)+pow(view.bounds.height-largeCenterPoint.y, 2));
        let largerMaskPath = UIBezierPath.init(ovalInRect: CGRectInset(CGRectMake(largeCenterPoint.x, largeCenterPoint.y, 1, 1), -largeRadiusFloat, -largeRadiusFloat));
        
        //表层图画
        let maskLayer = CAShapeLayer();
        maskLayer.path = largerMaskPath.CGPath;
        view.layer.mask = maskLayer;
        
        //基础动画
        let baseAnimation = CABasicAnimation.init(keyPath: "path");
        baseAnimation.duration = 0.5;
        baseAnimation.fromValue = smallMaskPath.CGPath;
        baseAnimation.toValue = largerMaskPath.CGPath;
        baseAnimation.delegate = self;
        maskLayer.addAnimation(baseAnimation, forKey: "path");
        
    }

然后今天说明的是:1:点击进行滑动动画;2:点击进行两次动画效果;3:基本动画;4:键值动画;5:转场动画

部分代码:

1:点击进行滑动动画;

(可以添加渐变效果)
    //# MARK:- 用UIView.animateWithDuration实现更逼真的运动效果
    func view1Clicked(tapGesture:UITapGestureRecognizer){
        //view1动画,向左移动100
        let theView = tapGesture.view!
        //usingSpringWithDamping 阻尼,范围0-1,阻尼越接近于0,弹性效果越明显
        UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            theView.frame = CGRect(x: theView.frame.origin.x+100, y: theView.frame.origin.y, width: theView.frame.size.width, height: theView.frame.height)
            }, completion: nil)
    }

2:点击进行两次动画效果;

//CATransaction 配置隐式动画
    func view1Clicked(tapGesture:UITapGestureRecognizer){
        
        CATransaction.begin()
        //CATranscation 属性
        //设置动画执行时间
        CATransaction.setAnimationDuration(1)
        //关闭隐式动画,这句话必须放在修改属性之前
        //        CATransaction.setDisableActions(true)
        //设置动画完成后的回调
        CATransaction.setCompletionBlock { () -> Void in
            NSLog("Animation complete")
        }
      
        CATransaction.setAnimationDuration(1)
        self.myLayer.backgroundColor = UIColor.greenColor().CGColor
        self.myLayer.opacity = 0.5
        var moveToPoint  = CGPoint(x: myLayer.position.x + 150, y: myLayer.position.y + 50)
        if(moveToPoint.x > view.frame.size.width) { moveToPoint.x -= view.frame.size.width}
        if(moveToPoint.y > view.frame.size.height) { moveToPoint.y -= view.frame.size.height}
        self.myLayer.position = moveToPoint
        
        
        CATransaction.commit()
        performSelector(#selector(TwoDonghuaViewController.pause), withObject: nil, afterDelay: 0.2)
        
    }

3:基本动画;

//移动
    func move(theView:UIView){
        /*
         可选的KeyPath
         transform.scale = 比例轉換
         transform.scale.x
         transform.scale.y
         transform.rotation = 旋轉
         transform.rotation.x
         transform.rotation.y
         transform.rotation.z
         transform.translation
         transform.translation.x
         transform.translation.y
         transform.translation.z
         
         opacity = 透明度
         margin
         zPosition
         backgroundColor 背景颜色
         cornerRadius 圆角
         borderWidth
         bounds
         contents
         contentsRect
         cornerRadius
         frame
         hidden
         mask
         masksToBounds
         opacity
         position
         shadowColor
         shadowOffset
         shadowOpacity
         shadowRadius
         
         */
        let baseAnimation = CABasicAnimation(keyPath: "position")
        //baseAnimation.fromValue 初始位置,如果不设就是当前位置
        let endPoint = CGPoint(x: theView.layer.position.x+100, y: theView.layer.position.y)
        baseAnimation.toValue = NSValue(CGPoint:endPoint)//绝对位置
        //baseAnimation.byValue = NSValue(CGPoint:CGPoint(x: 100, y: 0))//相对位置
        //动画其他属性
        baseAnimation.duration = 0.2
        baseAnimation.repeatCount = 1
        baseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)//加速运动
        //baseAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, 0, 0.9, 0.7)//自定义加速的曲线参数
        
        //这两个属性若不设置,动画执行后回复位
        baseAnimation.removedOnCompletion = false
        baseAnimation.fillMode = kCAFillModeForwards
        
        baseAnimation.delegate = self
        //可以在动画中缓存一些
        baseAnimation.setValue(NSValue(CGPoint: endPoint), forKey: "endPoint")
        baseAnimation.setValue(theView, forKey: "sender")
        
        //开始动画
        theView.layer.addAnimation(baseAnimation, forKey: "theViewMoveRight100")
    }

4:键值动画

func imageViewClicked(tapGesture:UITapGestureRecognizer){
        //键值动画
        //keyPath和basicAnimation的类型相同,@see BasicAnimationViewController.swift
        let keyframeAnimation = CAKeyframeAnimation(keyPath: "position")
        //弧线位置移动
        let path = CGPathCreateMutable()
        CGPathMoveToPoint(path, nil, 50, 50)
        CGPathAddCurveToPoint(path, nil, 50, 50, 700, 300, 30, 500)
        keyframeAnimation.path = path
        keyframeAnimation.calculationMode = kCAAnimationLinear
        
        //设置其他属性
        keyframeAnimation.duration = 1.0;
        //        keyframeAnimation.beginTime = CACurrentMediaTime() + 2;//设置延迟2秒执行
        tapGesture.view?.layer.addAnimation(keyframeAnimation, forKey: "keyframeAnimation1")
        tapGesture.view?.layer.presentationLayer()
        tapGesture.view?.layer.modelLayer()
    }

5:转场动画

func swipeTransition(subtype:String)->CATransition{
        let transfer = CATransition()
        /*
         kCATransitionFade:淡入淡出,默认效果
         kCATransitionMoveIn:新视图移动到就是图上方
         kCATransitionPush:新视图推开旧视图
         kCATransitionReveal:移走旧视图然后显示新视图
         
         //苹果未公开的私有转场效果
         cube:立方体
         suckEffect:吸走的效果
         oglFlip:前后翻转效果
         rippleEffect:波纹效果
         pageCurl:翻页起来
         pageUnCurl:翻页下来
         cameraIrisHollowOpen:镜头开
         cameraIrisHollowClose:镜头关
         */
        let types = [kCATransitionFade,kCATransitionMoveIn,kCATransitionPush,kCATransitionReveal,"cube","suckEffect","oglFlip","rippleEffect","pageCurl","pageUnCurl","cameraIrisHollowOpen","cameraIrisHollowClose"]
        let type = types[Int(arc4random()%11)]
        transfer.type = type
        NSLog("动画类型。。。。%@", type)
        transfer.subtype = subtype
        transfer.duration = 1
        return transfer
    }

源代码:http://download.csdn.net/detail/hbblzjy/9647803,敬请关注,(感觉不错,想要代码的,请留言或关注我)。。。

Swift基础之Animation动画研究的更多相关文章

  1. 【Android 基础】Animation 动画介绍和实现

    在前面PopupWindow 实现显示仿腾讯新闻底部弹出菜单有用到Animation动画效果来实现菜单的显示和隐藏,本文就来介绍下吧. 1.Animation 动画类型 Android的animati ...

  2. Swift 实现iOS Animation动画教程

    这是一篇翻译文章.原文出处:http://www.raywenderlich.com/95910/uiview-animation-swift-tutorial 动画( animation)是iOS用 ...

  3. android动画效果编程基础--Android Animation

    动画效果编程基础--Android Animation 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 tran ...

  4. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

  5. Android物业动画研究(Property Animation)彻底解决具体解释

     前p=1959">Android物业动画研究(Property Animation)全然解析具体解释上已经基本展示了属性动画的核心使用方法: ObjectAnimator实现动画 ...

  6. Android基础夯实--重温动画(二)之Frame Animation

    心灵鸡汤:天下事有难易乎,为之,则难者亦易矣:不为,则易者亦难矣. 摘要 当你已经掌握了Tween Animation之后,再来看Frame Animation,你就会顿悟,喔,原来Frame Ani ...

  7. Android基础夯实--重温动画(三)之初识Property Animation

    每个人都有一定的理想,这种理想决定着他的努力和判断的方向.就在这个意义上,我从来不把安逸和快乐看作生活目的的本身--这种伦理基础,我叫它猪栏的理想.--爱因斯坦 一.摘要 Property Anima ...

  8. Android基础夯实--重温动画(一)之Tween Animation

    心灵鸡汤:真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自己的声音,走出属于自己的道路. 摘要 不积跬步,无以至千里:不积小流,无以成江海.学习任何东西我们都离不开扎实的基础知识 ...

  9. Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解

    只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...

随机推荐

  1. 51Nod 1331 狭窄的通道

    有一个长为L的狭窄通道,我们假设这个通道在x轴上,其两个出口分别在x=0与x=L处.在这个通道里有N只狼,第i只狼有一个初始位置ai,它想到达位置bi(0<=i=L处空间足够大可以装下任意数量的 ...

  2. springmvc上传文件方法及注意事项

    本文基于注解的配置,敬请留意  基于注解整合 一.springmvc为我们提供两种上传方式配置: org.springframework.web.multipart.commons.CommonsMu ...

  3. html高度塌陷以及定位的理解

    高度塌陷的含义: 父元素的高度,默认被子元素撑开,目前来讲box2多高,box1就多高.此时如果子元素设置浮动,则会导致其完全脱离文档流,子元素脱离文档流将无法撑开父元素, 导致父元素的高度丢失,就是 ...

  4. day5 liaoxuefeng---访问数据库、web开发、异步IO

    一.访问数据库 二.web开发 三.异步IO

  5. Windows Server 2008 R2服务器系统安全设置参考指南

    Server 2008 R2服务器系统安全设置参考指南  重点比较重要的几部 1.更改默认administrator用户名,复杂密码 2.开启防火墙 3.安装杀毒软件 1)新做系统一定要先打上补丁(升 ...

  6. jquery传值与判断

    js判断是否包含字符串 var str="Hello world!" var s = str.indexOf("Hello") 存在则s>-1不存在则是s ...

  7. Flexible DEMO 实现手淘H5页面的终端适配

    <!DOCTYPE html> <html> <head> <title>淘宝flexiblejs</title> <meta cha ...

  8. 我与android的缘分

    android的开始 本人是一名大三的学生,大一大二主要学习的是php后台开发,在大一的时候做过一些小的网站系统,也参加过一些大学生计算机相关的比赛.这次开始着手于安卓开发,也是一时的兴起.因为跟我们 ...

  9. sololearn的c++学习记录_4m11d

    Function Templates Functions and classes help to make programs easier to write, safer, and more main ...

  10. 深入浅出低功耗蓝牙(BLE)协议栈

    深入浅出低功耗蓝牙(BLE)协议栈 BLE协议栈为什么要分层?怎么理解蓝牙"连接"?如果蓝牙协议只有ATT没有GATT会发生什么? 协议栈框架 一般而言,我们把某个协议的实现代码称 ...