Last active
October 28, 2016 09:48
-
-
Save katoy/2fd35b6039619296d14f to your computer and use it in GitHub Desktop.
draw line in playground (Xcode 6.1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See http://techlife.cookpad.com/entry/2014/11/12/170041 | |
// | |
// 本来はplayground用に用意されているXCPlaygroundフレームワークのXCPShowViewを使って | |
// Timelineに表示することが可能ですが、現行のXcode6.1でiOS用にUIKitを使って表示した場合 | |
// コンソールにエラーが出てしまうため使用していません。 | |
// | |
import UIKit | |
// ビューのサイズ | |
let size = CGSize(width: 300, height: 300) | |
// UIViewを生成 | |
let view:UIView = UIView(frame: CGRect(origin: CGPointZero, size: size)) | |
view.backgroundColor = UIColor(white: 0.9, alpha: 1.0) | |
// CoreGraphicsで描画する | |
UIGraphicsBeginImageContextWithOptions(size, false, 0) | |
// 描画する | |
let path = UIBezierPath() | |
path.moveToPoint(CGPointMake(50, 100)) | |
path.addLineToPoint(CGPointMake(150, 100)) | |
UIColor.orangeColor().setStroke() | |
path.stroke() | |
UIColor.blueColor().setStroke() | |
var shift = 0 | |
let delta = 15 | |
while shift <= 300 { | |
var path2 = UIBezierPath() | |
let t = CGFloat(shift) | |
path2.moveToPoint(CGPointMake(t, 300)) | |
path2.addLineToPoint(CGPointMake(0, t)) | |
path2.stroke() | |
// move our shift a little bit | |
shift += delta | |
} | |
// viewのlayerに描画したものをセットする | |
view.layer.contents = UIGraphicsGetImageFromCurrentImageContext().CGImage | |
UIGraphicsEndImageContext() | |
// PlaygroundのTimelineに表示するためのview | |
let preview = view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment