OP logo 애니메이션 설치

17628 단어 Swift

모티프


swift로 애니메이션을 만드는 프로그램 라이브러리를 만들고 싶어서요.

이루어지다


UIView를 상속받은 logo Animation View 클래스를 생성합니다.
logoAnimationView.swift

import UIKit

class logoAnimationView: UIView{

    //public
    var text: NSString = ""
    var font: UIFont = UIFont.boldSystemFontOfSize(40)
    var textColor: UIColor = UIColor.brownColor()
    var animationDuration: Double = 4.0

    //private
    private var contentView: UIView = UIView()
    private var stopAnimationFlag: Bool = false
    private var animationIndex: NSInteger = 0
    private var labelArray: Array<UILabel> = Array<UILabel>()


    //Class Method
    func startAnimation(){
        self.reloadView()

    }

    func stopAnimation(){
        self.stopAnimationFlag = false
    }

    //Private Method

    //初期化
    private func initializeView(){
        for subView: UILabel in self.labelArray {
            subView.removeFromSuperview()
        }
        labelArray = Array<UILabel>()
        stopAnimationFlag = false

        contentView = UIView(frame: CGRectMake(0,0,10,10))
        contentView.backgroundColor = UIColor.clearColor()
        self.addSubview(contentView)
    }

    //Viewの描画
    private func reloadView(){
        self.initializeView()

        let startX = CGRectGetWidth(self.frame)
        let startY = CGRectGetHeight(self.frame)

        //textの文字を\nを区切りにして分解
        var lines = text.componentsSeparatedByString("\n")

        //Label作成処理
        for var i=0; i<lines.count; i++ {

            //CGFloatとIntでは下で計算できないのでここで型変換
            var n_col:CGFloat = CGFloat(i)

            //linesの文字を1文字ずつに分解
            var chars = lines[i].characters.map{ String($0) }

            for var j=0; j<chars.count; j++ {

                var n_row:CGFloat = CGFloat(j)

                //X開始位置はViewの大きさ-Labelの幅:ステップは列の最大数で割ってる、Y開始位置は0:ステップは行の最大数で割ってる
                var label: UILabel = UILabel(frame:
                    CGRectMake(
                        CGRectGetWidth(self.frame)-(n_col+1)*CGRectGetWidth(self.frame)/3,
                        n_row*CGRectGetHeight(self.frame)/4,
                        CGRectGetWidth(self.frame)/3,
                        CGRectGetHeight(self.frame)/4))
                label.backgroundColor = UIColor.clearColor()
                label.text = chars[j]
                label.font = self.font
                label.textColor = self.textColor
                label.sizeToFit()
                label.textAlignment = NSTextAlignment.Center

                self.contentView.addSubview(label) //viewにlabelを追加
                self.labelArray.append(label) //配列にも追加

                self.executeAnimation(animeID)
            }
        }
    }

    //アニメーション実行
    private func executeAnimation(id:Int){

        self.alpha = 0

        var duration: Double = self.animationDuration

        //アニメーションの設定
        for label: UILabel in self.labelArray {
            UIView.animateWithDuration(duration, animations: {

                self.alpha = 1

            }
                ,completion: { (finished: Bool) in
            })
        }
    }
}

사용 예


200*200에서 상위 뷰를 그립니다.
viewController.swift
import UIKit

class ViewController: UIViewController {

    var viewSize: CGFloat = 200

    override func viewDidLoad() {
        super.viewDidLoad()

        //Viewの描画位置を調整
        var animationLabel: logoAnimationView = logoAnimationView(frame: CGRectMake(
            //0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)))
            CGRectGetWidth(self.view.frame)/2 - viewSize/2, CGRectGetHeight(self.view.frame)/2-viewSize/2, viewSize, viewSize))
        animationLabel.text = "ご注文は\nうさぎ\nですか?"
        animationLabel.font = UIFont(name: "HannariMincho", size: 40)!

        self.view.addSubview(animationLabel)

        animationLabel.startAnimation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

완성



애니메이션이 아직 멀었기 때문에 도식, 섀도우, 문자의 동작 등을 개선하고 싶다.
어쨌든 프로그램 라이브러리는 처음이야.

좋은 웹페이지 즐겨찾기