ios - Triangle UIView - Swift -
so i'm making game in dropping objects have destroyed spike(triangle) @ bottom of screen user.
i cannot work out how make uiview triangle. have been able make work rectangle this:
let barrier = uiview(frame: cgrect(x:125, y: 650, width: 130, height:20)) barrier.backgroundcolor = uicolor.orangecolor() view.addsubview(barrier)
and has worked. cannot work out how make triangle. reason want uiview because im using collisions on , user move it. have tried png triangle detects collision border of image not start of triangle.
i have tried doesn't work...
let square = uiview(frame: cgpathmovetopoint(path, nil, 50, 0), cgpathaddlinetopoint(path, nil, 100, 50), cgpathaddlinetopoint(path, nil, 0, 100)) square.backgroundcolor = uicolor.purplecolor() view.addsubview(square)
any , appreciated,
thanks,
alex
updated swift 3:
class triangleview : uiview { override init(frame: cgrect) { super.init(frame: frame) } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) } override func draw(_ rect: cgrect) { guard let context = uigraphicsgetcurrentcontext() else { return } context.beginpath() context.move(to: cgpoint(x: rect.minx, y: rect.maxy)) context.addline(to: cgpoint(x: rect.maxx, y: rect.maxy)) context.addline(to: cgpoint(x: (rect.maxx / 2.0), y: rect.miny)) context.closepath() context.setfillcolor(red: 1.0, green: 0.5, blue: 0.0, alpha: 0.60) context.fillpath() } }
swift 2:
import uikit class triangleview : uiview { override init(frame: cgrect) { super.init(frame: frame) } required init(coder adecoder: nscoder) { super.init(coder: adecoder) } override func drawrect(rect: cgrect) { var ctx : cgcontextref = uigraphicsgetcurrentcontext() cgcontextbeginpath(ctx) cgcontextmovetopoint(ctx, cgrectgetminx(rect), cgrectgetmaxy(rect)) cgcontextaddlinetopoint(ctx, cgrectgetmaxx(rect), cgrectgetmaxy(rect)) cgcontextaddlinetopoint(ctx, (cgrectgetmaxx(rect)/2.0), cgrectgetminy(rect)) cgcontextclosepath(ctx) cgcontextsetrgbfillcolor(ctx, 1.0, 0.5, 0.0, 0.60); cgcontextfillpath(ctx); } }
this start minx, maxy;
draw line start maxx, maxy;
draw line maxx,maxy maxx/2, miny;
close path start location.
next part sets color want use. in example 255,127,0, alpha 0.6 fill path drew above set color.
then in view controller
swift 3:
class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() let triangle = triangleview(frame: cgrect(x: 10, y: 20, width: 25 , height: 30)) triangle.backgroundcolor = .white view.addsubview(triangle) } override func didreceivememorywarning() { super.didreceivememorywarning() } }
swift 2:
class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. let triangle = triangleview(frame: cgrectmake(10, 20, 25, 30)) triangle.backgroundcolor = .whitecolor() view.addsubview(triangle) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
however, going cause same problem frame of view still going rectangle. uikit works rectangles, have use framework, sprite kit.
Comments
Post a Comment