继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

元芳,iOS 四大邪术之一,你怎么看?

MYYA
关注TA
已关注
手记 300
粉丝 75
获赞 326

标题党果然吸流量

现在穷,自打夏商周开始,祖辈应该都是穷人 ,自打夏商周开始,祖辈就没有一个买过车的  ---这句如果没责任就是我扯的
<br />
文文笔戳的一B。文字生硬。xx 死的早。,体育老师也相继。。。,语文自学

700

Paste_Image.png

700

Paste_Image.png

以上讲解通通都没有

414

Paste_Image.png

来吧。拥抱iOS 四大core Animation 之一 CABasicAnimation

先上demo

700

CABaseAnimation.gif

iOS中实现一个动画可以很简单,在view层面上有封装的方法

UIView.animateWithDuration(<#T##duration: NSTimeInterval##NSTimeInterval#>, animations: <#T##() -> Void#>)

但是它不能控制动画的暂停和组合,所以就需要用到CoreAnimation了。

iOS中的动画主要分为:
基础动画(CABasicAnimation)
关键帧动画(CAKeyFrameAnimation)
动画组(CAAnimationGroup)
转场动画(CATransition)

打字好累。上代码和github 链接,完事也是该买包泡面吃了。犒赏自己了!

////  LessonTest.swift////  Created by liujunbin on 16/8/12.//  Copyright © 2016年 liujiacuncunzhang. All rights reserved.//import UIKit

  class LKFLessonTest: UIViewController {  //关键字懒加载lazy 。里面调用class的方法需要前面加self,不然会报错,干货不谢。这里布局不用VFL 写了。就1个uivew 而已。
  lazy var box : UIView = {
      let v = UIView(frame: CGRect(x: 100, y: 100, width: 30, height: 30))
      v.backgroundColor = self.randowColor()    return v
}()

override func viewDidLoad() {    super.viewDidLoad()    self.view.backgroundColor = UIColor.gray_light()    self.view.addSubview(box)//        /动画不会真正改变opacity的值,在动画结束后要手动更改//        CABasicAnimation//        CAKeyframeAnimation//        CAAnimationGroup//        CATransition//        CAAnimation}//改变动画为随机的颜色func changColor(vw : CALayer){
    let c = randowColor().CGColor
    let ca = CABasicAnimation(keyPath: "backgroundColor")
    ca.toValue = c
    ca.duration = 1
    ca.delegate = self
    ca.setValue(c, forKey: "endcolor")
    vw.addAnimation(ca, forKey: "backgroundColor")
}//鼠标到哪里。uivew 更到哪里func positionLayer(vw : CALayer , p : CGPoint){

    let ca = CABasicAnimation(keyPath: "position")
    ca.duration = 1
    ca.toValue = NSValue(CGPoint: p)
    ca.delegate = self
    ca.setValue(NSValue(CGPoint: p), forKey: "endPosition")
    vw.addAnimation(ca, forKey: "position")

}//旋转func rotate(vw:CALayer){

    let ca = CABasicAnimation(keyPath: "transform.rotation.z")
    ca.toValue = NSNumber(float: Float(M_PI) * 3)

    ca.duration = 1
    vw.addAnimation(ca, forKey: "transform.rotation.z")
}//动画停止的代理。当然还有其他的 forexample animationDidStartoverride func animationDidStop(anim: CAAnimation, finished flag: Bool) {

    print("animationDidStop \\\\\\\\(flag)")    self.box.layer.opacity = 1
    if let a = anim.valueForKey("endPosition") {        self.box.layer.position = (a as? NSValue)?.CGPointValue() ?? CGPointZero
    }
    print(anim.valueForKey("endcolor"))    if let c = anim.valueForKey("endcolor") {        self.box.layer.backgroundColor = c as! CGColor
    }

}//随机个颜色func randowColor() -> UIColor{
    let r = CGFloat(arc4random() % 256)
    let g = CGFloat(arc4random() % 256)
    let b = CGFloat(arc4random() % 256)    return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1)
}//重写捕获的touchesBegan 。 手指按下的那一刻override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    print("touchesBegan 开始动画")    if let p = touches.first {
        let cp = p.locationInView(self.view)

        positionLayer(self.box.layer, p: cp)
        rotate(self.box.layer)
        changColor(self.box.layer)
    }

}

}

按照惯例。文章稿纸的地址详见
https://github.com/976500133/IOS-Dev-With-Swift/tree/master/learn053



作者:二月长河
链接:https://www.jianshu.com/p/07843cf28b13


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP