Swfit 之SimpleAnimation和UIBezierPath

SimpleAnimation和UIBezierPath

SimpleAnimation

@objc func rightButtonItem() -> Void{
        UIView.animate(withDuration: 1.0) {
            self.animationView.frame = CGRect(x: self.view.frame.width / 2 - 50, y: 100, width: 100, height: 100)
            self.animationView.alpha = 1.0
            self.animationView.backgroundColor = UIColor.red
        }
    }
 
 <!--動(dòng)畫改變視圖的x,y,width,height-->   
    func animation(x: CGFloat,y: CGFloat, width: CGFloat, height: CGFloat) -> Void {
        UIView.animate(withDuration: 1.0) {
            var rect = self.animationView.frame
            rect.origin.x += x
            rect.origin.y += y
            rect.size.width += width
            rect.size.height += height
            self.animationView.frame = rect
        };
    }
    <!--動(dòng)畫改變視圖的alpha-->
    func animationAlpha(alpha: CGFloat) -> Void {
        UIView.animate(withDuration: 1.0, animations: {
            self.animationView.alpha = alpha
        }) { (success) in
//            alpha變0 就要去移除view
//            self.animationView.removeFromSuperview()
        }
    }
    <!--動(dòng)畫改變視圖的color-->
    func animationBackgroundColor(color: UIColor) -> Void {
        UIView.animate(withDuration: 1.0) {
            self.animationView.backgroundColor = color
        }
    }

keyFrameAnimation

   <!--扇頁旋轉(zhuǎn)-->
func drawAnimation() -> Void {
        let animation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        animation.fromValue = 0
        animation.toValue = CGFloat(Double.pi * 2)
        animation.duration = 3.0
        animation.autoreverses = false;
        animation.fillMode = CAMediaTimingFillMode.forwards;
        animation.repeatCount = MAXFLOAT;
        self.ellipseView.layer.add(animation, forKey: nil)
    }
    
 <!--弧形動(dòng)效-->
    func animationPosition(value: CGFloat) -> Void {
        //        添加動(dòng)畫
        baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
        baseAnimation.duration = 2   //持續(xù)時(shí)間
        baseAnimation.fromValue = 0  //開始值
        baseAnimation.toValue = value   //結(jié)束值
        //        保持運(yùn)動(dòng)后的位置不變
        baseAnimation.isRemovedOnCompletion = false
        baseAnimation.fillMode = CAMediaTimingFillMode.forwards
        animationView.shapeLayer.add(baseAnimation, forKey: "strokeEndAnimation")
    }
圓弧動(dòng)效
 let bezierPath = UIBezierPath.init()
    let shapeLayer = CAShapeLayer.init()
    
    <!--有些屬性必須要在draw 方法里才能生效-->
    override func draw(_ rect: CGRect) {
        // Drawing code
        let radius = rect.width / 2.0 - 20        //確定半徑
//        繪制圓弧
        bezierPath.addArc(withCenter: CGPoint(x: 100, y: 100), radius: radius, startAngle: CGFloat(0.25 + Double.pi / 2.0), endAngle:CGFloat(2 * Double.pi + Double.pi / 2.0 - 0.25), clockwise: true)
        bezierPath.lineWidth = 5
        UIColor.red.setStroke()      // 設(shè)置路徑顏色
        bezierPath.stroke()
//        繪制添加動(dòng)畫路徑蒙層
        shapeLayer.lineWidth = 5
        shapeLayer.strokeColor = UIColor.green.cgColor
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.path = bezierPath.cgPath
        layer.addSublayer(shapeLayer)
    }
扇頁動(dòng)效
    override func draw(_ rect: CGRect) {
        
//        繪制中心原點(diǎn)
        let circleBezier = UIBezierPath.init()
        circleBezier.addArc(withCenter: CGPoint(x: 100, y: 100), radius: 5, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: true)
        UIColor.green.setFill()
        circleBezier.close()
        circleBezier.lineWidth = 1
        circleBezier.fill()
        

        // 繪制扇葉
        let ellipseView = UIView.init(frame: CGRect(x: 0, y: 0, width: 80, height: 5))
        ellipseView.center = CGPoint(x: 100, y: 40)
        ellipseView.backgroundColor = UIColor.white
        self.addSubview(ellipseView)
        
        let ellipseView2 = UIView.init(frame: CGRect(x: 0, y: 0, width: 80, height: 5))
        ellipseView2.center = CGPoint(x: 50, y: (50 + sqrt(7500)))
        ellipseView2.backgroundColor = UIColor.white
        self.addSubview(ellipseView2)
        
        let ellipseView3 = UIView.init(frame: CGRect(x: 0, y: 0, width: 80, height: 5))
        ellipseView3.center = CGPoint(x: 150, y: (50 + sqrt(7500)))
        ellipseView3.backgroundColor = UIColor.white
        self.addSubview(ellipseView3)
        
        let path = UIBezierPath.init(ovalIn:ellipseView.bounds)
        let pathLayer = CAShapeLayer.init()
        pathLayer.lineWidth = 1
        pathLayer.strokeColor = UIColor.green.cgColor
        pathLayer.path = path.cgPath
        pathLayer.fillColor = nil
        ellipseView.layer.addSublayer(pathLayer)
        
        let path2 = UIBezierPath.init(ovalIn:ellipseView2.bounds)
        let pathLayer2 = CAShapeLayer.init()
        pathLayer2.lineWidth = 1
        pathLayer2.strokeColor = UIColor.green.cgColor
        pathLayer2.path = path2.cgPath
        pathLayer2.fillColor = nil
        ellipseView2.layer.addSublayer(pathLayer2)
        
        let path3 = UIBezierPath.init(ovalIn:ellipseView3.bounds)
        let pathLayer3 = CAShapeLayer.init()
        pathLayer3.lineWidth = 1
        pathLayer3.strokeColor = UIColor.green.cgColor
        pathLayer3.path = path3.cgPath
        pathLayer3.fillColor = nil
        ellipseView3.layer.addSublayer(pathLayer3)
        
        UIView.animate(withDuration: 0) {
            ellipseView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2.0))
            ellipseView2.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 4.0 + (1 / 12)))
            ellipseView3.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 4.0 * 3 - (1 / 12)))
        }
        
        
        
    }
    
扇形
 override func draw(_ rect: CGRect) {
        // Drawing code
        let circleBezier = UIBezierPath.init()
        circleBezier.addArc(withCenter: CGPoint(x: 100, y: 100), radius: 100, startAngle: CGFloat(Double.pi / 2.0 - 0.1), endAngle: CGFloat(Double.pi / 2.0 + 0.1), clockwise: true)
        UIColor.green.setStroke()
        circleBezier.addLine(to: CGPoint(x: 100, y: 100))
        circleBezier.close()
        circleBezier.lineWidth = 1
        circleBezier.stroke()

    }

直通車: https://github.com/princeSmall/Swift_Animations

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市同波,隨后出現(xiàn)的幾起案子鳄梅,更是在濱河造成了極大的恐慌,老刑警劉巖未檩,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件戴尸,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡冤狡,警方通過查閱死者的電腦和手機(jī)孙蒙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悲雳,“玉大人挎峦,你說我怎么就攤上這事『掀埃” “怎么了坦胶?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長晴楔。 經(jīng)常有香客問我迁央,道長,這世上最難降的妖魔是什么滥崩? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任岖圈,我火速辦了婚禮,結(jié)果婚禮上钙皮,老公的妹妹穿的比我還像新娘蜂科。我一直安慰自己,他們只是感情好短条,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布导匣。 她就那樣靜靜地躺著,像睡著了一般茸时。 火紅的嫁衣襯著肌膚如雪贡定。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天可都,我揣著相機(jī)與錄音缓待,去河邊找鬼蚓耽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛旋炒,可吹牛的內(nèi)容都是我干的步悠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼瘫镇,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼鼎兽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起铣除,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤谚咬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后尚粘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體序宦,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年背苦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了互捌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡行剂,死狀恐怖秕噪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情厚宰,我是刑警寧澤腌巾,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站铲觉,受9級(jí)特大地震影響澈蝙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撵幽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一灯荧、第九天 我趴在偏房一處隱蔽的房頂上張望泡态。 院中可真熱鬧甥材,春花似錦蘑拯、人聲如沸务甥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽兼犯。三九已至乳规,卻和暖如春强衡,著一層夾襖步出監(jiān)牢的瞬間擦秽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留感挥,地道東北人缩搅。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像链快,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子眉尸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354