LoginSignup
9
8

More than 5 years have passed since last update.

線を引くのに2時間😩

Last updated at Posted at 2016-10-19

大変だったので備忘メモ😩

ViewController.swift
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {

        let shapeLayer = CAShapeLayer()

        let uiPath = UIBezierPath()
        uiPath.moveToPoint(CGPointMake(5, 5))       // ここから
        uiPath.addLineToPoint(CGPointMake(15, 15))  // ここまで線を引く

        shapeLayer.strokeColor = UIColor.blueColor().CGColor  // 微妙に分かりにくい。色は要指定。
        shapeLayer.path = uiPath.CGPath  // なんだこれは

        // 作成したCALayerを画面に追加
        view.layer.addSublayer(shapeLayer)
    }
}

ちなみに線をクラスに切り出すとこんな感じ:

ViewController.swift
import UIKit

class ViewController: UIViewController {
    var myShapeLayer: MyShapeLayer

    override func viewDidLoad() {

        myShapeLayer = MyShapeLayer()
        view.layer.addSublayer(myShapeLayer)
    }

    ...

    shapeLayer.position.x += 10.0  // Core Animationでヌルッと動く。これがやりたかった。

    ...
}
MyShapeLayer.swift
import UIKit

class MyShapeLayer: CAShapeLayer {
    override init() {
        super.init()

        let uiPath = UIBezierPath()
        uiPath.moveToPoint(CGPointMake(5, 5))
        uiPath.addLineToPoint(CGPointMake(15, 15))

        strokeColor = UIColor.blueColor().CGColor
        path = uiPath.CGPath
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

方法はあと2つあるそうです:

【iOS】drawRect、CALayer、UIImageViewで描画速度を検証してみた - Qiita

参考

【iOS Swift入門 #207】CAShapeLayerで円を描く。くるくるする | Swift,Objective-Cプログラミング ~ iOS ~


ブログやってます:http://weed.nagoya

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8