Swift 簡單動畫

一: 效果

a: 圖形做 bounds動畫
b: 線條做跟隨動畫
c: 圓點逐漸呈現(xiàn)動畫
d: 圖標做軌跡動畫

Animation.gif

二:代碼

static let animationDucation: CFTimeInterval = 3

override func viewDidLoad() {
    super.viewDidLoad()
    
    
    // MARK: 矩形 動畫部分
    let path = UIBezierPath()
    
    path.move(to: CGPoint(x: 20, y: 300))       // 起點
    
    path.addLine(to: CGPoint(x: 40, y: 100))
    path.addLine(to: CGPoint(x: 80, y: 200))
    path.addLine(to: CGPoint(x: 150, y: 60))
    path.addLine(to: CGPoint(x: 200, y: 100))
    
    path.addLine(to: CGPoint(x: 340, y: 300))   // 右終點
    path.addLine(to: CGPoint(x: 20, y: 300))   // 閉圈
    
    let contentLayer = CALayer()
    contentLayer.frame = self.view.frame
    
    // 1: 畫一個自下而上的漸變圖
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = CGRect(x: 0, y: 0, width: 0, height: 300)
    gradientLayer.startPoint = CGPoint(x: 0, y: 1)
    gradientLayer.endPoint = CGPoint(x: 0, y: 0)
    gradientLayer.colors = [
        UIColor(red: 185 / 255.0, green: 185 / 255.0, blue: 200 / 255.0, alpha: 1).cgColor,
        UIColor(red: 240 / 255.0, green: 240 / 255.0, blue: 240 / 255.0, alpha: 1).cgColor]
    gradientLayer.locations = [0.01, 0.5]   // colors 元素的起點
    contentLayer.addSublayer(gradientLayer)
    
    // 2: 用貝塞爾構建的形狀 貼住漸變圖(圖形以外不可見)
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.fillColor = UIColor.green.cgColor
    contentLayer.mask = shapeLayer
    
    self.view.layer.addSublayer(contentLayer)
    
    // Animation
    let animation = CABasicAnimation(keyPath: "bounds")
    animation.duration = ProfileViewController.animationDucation
    // toValue width 是 frame width的 2 倍,不然動畫不完全 不知道為什么
    animation.toValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: 680, height: 300))
    animation.timingFunction = CAMediaTimingFunction(name: .linear)
    animation.fillMode = .forwards
    animation.autoreverses = false
    animation.isRemovedOnCompletion = false
    gradientLayer.add(animation, forKey: "bounds")
    
    
    // MARK: 滾動線條 部分
    let linePath = UIBezierPath()
    
    linePath.move(to: CGPoint(x: 20, y: 300))       // 起點
    linePath.addLine(to: CGPoint(x: 40, y: 100))
    
    linePath.addLine(to: CGPoint(x: 80, y: 200))
    linePath.addLine(to: CGPoint(x: 150, y: 60))
    linePath.addLine(to: CGPoint(x: 200, y: 100))
    linePath.addLine(to: CGPoint(x: 340, y: 300))   // 右終點
    
    let lineShapeLayer = CAShapeLayer()
    lineShapeLayer.path = linePath.cgPath
    lineShapeLayer.strokeColor = UIColor.red.cgColor
    lineShapeLayer.fillColor = nil
    lineShapeLayer.lineWidth = 1
    self.view.layer.addSublayer(lineShapeLayer)
    
    // Animation
    let lineAnimation = CABasicAnimation(keyPath: "strokeEnd")
    lineAnimation.duration = ProfileViewController.animationDucation
    lineAnimation.fromValue = 0
    lineAnimation.toValue = 1
    lineAnimation.timingFunction = CAMediaTimingFunction(name: .easeIn)
    lineAnimation.fillMode = .forwards
    lineAnimation.autoreverses = false
    lineAnimation.isRemovedOnCompletion = false
    lineShapeLayer.add(lineAnimation, forKey: "strokeEnd")
    
    // MARK: 小圓點部分
    
    let circlePath = UIBezierPath()
    circlePath.move(to: CGPoint(x: 40, y: 100))
    circlePath.addArc(withCenter: CGPoint(x: 40, y: 100), radius: 10, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
    
    circlePath.move(to: CGPoint(x: 80, y: 200))
    circlePath.addArc(withCenter: CGPoint(x: 80, y: 200), radius: 10, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
    
    circlePath.move(to: CGPoint(x: 150, y: 60))
    circlePath.addArc(withCenter: CGPoint(x: 150, y: 60), radius: 10, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
    
    circlePath.move(to: CGPoint(x: 200, y: 100))
    circlePath.addArc(withCenter: CGPoint(x: 200, y: 100), radius: 10, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
    
    let circleShapeLayer = CAShapeLayer()
    circleShapeLayer.path = circlePath.cgPath
    circleShapeLayer.fillColor = UIColor(red: 185 / 255.0, green: 185 / 255.0, blue: 200 / 255.0, alpha: 1).cgColor
    circleShapeLayer.lineWidth = 1
    self.view.layer.addSublayer(circleShapeLayer)
    
    // Animation
    let circleAnimation = CABasicAnimation(keyPath: "opacity")
    circleAnimation.duration = ProfileViewController.animationDucation
    circleAnimation.fromValue = 0.3
    circleAnimation.toValue = 1
    circleAnimation.timingFunction = CAMediaTimingFunction(name: .easeIn)
    circleAnimation.fillMode = .forwards
    circleAnimation.autoreverses = false
    circleAnimation.isRemovedOnCompletion = false
    circleShapeLayer.add(circleAnimation, forKey: "opacity")

   // MARK: 小圖標部分
     let carLayer: CALayer = CALayer()
    carLayer.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
    carLayer.contents = UIImage.init(named: "sharelogo")!.cgImage

    let carAnimation = CAKeyframeAnimation.init(keyPath: "position")
    carAnimation.path = linePath.cgPath
    carAnimation.timingFunction = CAMediaTimingFunction(name: .linear)
    carAnimation.duration = 6
    carAnimation.repeatCount = MAXFLOAT
    carAnimation.autoreverses = false
    carAnimation.calculationMode = .cubicPaced
    carAnimation.rotationMode = .rotateAuto
    self.view.layer.addSublayer(carLayer)
    carLayer.add(carAnimation, forKey: "carAnimation")

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市喂窟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌宠漩,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件懊直,死亡現(xiàn)場離奇詭異扒吁,居然都是意外死亡,警方通過查閱死者的電腦和手機室囊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門雕崩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人融撞,你說我怎么就攤上這事晨逝。” “怎么了懦铺?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵捉貌,是天一觀的道長。 經常有香客問我冬念,道長趁窃,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任急前,我火速辦了婚禮醒陆,結果婚禮上,老公的妹妹穿的比我還像新娘裆针。我一直安慰自己刨摩,他們只是感情好,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布世吨。 她就那樣靜靜地躺著澡刹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪耘婚。 梳的紋絲不亂的頭發(fā)上罢浇,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機與錄音,去河邊找鬼嚷闭。 笑死攒岛,一個胖子當著我的面吹牛,可吹牛的內容都是我干的胞锰。 我是一名探鬼主播灾锯,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼嗅榕!你這毒婦竟也來了挠进?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤誊册,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后暖璧,有當地人在樹林里發(fā)現(xiàn)了一具尸體案怯,經...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年澎办,在試婚紗的時候發(fā)現(xiàn)自己被綠了嘲碱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡局蚀,死狀恐怖麦锯,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情琅绅,我是刑警寧澤扶欣,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站千扶,受9級特大地震影響料祠,放射性物質發(fā)生泄漏。R本人自食惡果不足惜澎羞,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一髓绽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧妆绞,春花似錦顺呕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至图焰,卻和暖如春忌卤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背楞泼。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工驰徊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留笤闯,地道東北人。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓棍厂,卻偏偏與公主長得像颗味,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子牺弹,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355