Zog*_*Zog 9 uiview ios cgrect swift
我正在制作一个游戏并需要一个底部的尖峰,我决定通过UIView和碰撞来做.我正在迅速编码.
我目前有一个广场:
//Object Setup
let square = UIView(frame: CGRect(x:100, y:100, width: 100, height: 100))
square.backgroundColor = UIColor.purpleColor()
view.addSubview(square)
Run Code Online (Sandbox Code Playgroud)
我想要一个三角形,我确实有一个可用于三角形的图像,但图像是方形的,所以当碰到图像边界而不是实际的三角形边界时,碰撞就是肯定的,请告知如何处理图像或我是如何得到的
谢谢
亚历克斯
Mic*_*ael 34
UIView调用子类的新文件TriangleViewTriangleView课程中TriangleView通过XIB或以编程方式import UIKit
class TriangleView: UIView {
override func draw(_ rect: CGRect) {
// Get Height and Width
let layerHeight = layer.frame.height
let layerWidth = layer.frame.width
// Create Path
let bezierPath = UIBezierPath()
// Draw Points
bezierPath.move(to: CGPoint(x: 0, y: layerHeight))
bezierPath.addLine(to: CGPoint(x: layerWidth, y: layerHeight))
bezierPath.addLine(to: CGPoint(x: layerWidth / 2, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: layerHeight))
bezierPath.close()
// Apply Color
UIColor.green.setFill()
bezierPath.fill()
// Mask to Path
let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
layer.mask = shapeLayer
}
}
Run Code Online (Sandbox Code Playgroud)

这里有教程:
http://jamesonquave.com/blog/drawing-custom-views-with-swift-andrew-vanwagoner/
使用这样的代码:
func drawPlayPathTo(context: CGContextRef, boundedBy rect: CGRect) {
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
CGContextMoveToPoint(context, rect.width / 4, rect.height / 4)
CGContextAddLineToPoint(context, rect.width * 3 / 4, rect.height / 2)
CGContextAddLineToPoint(context, rect.width / 4, rect.height * 3 / 4)
CGContextAddLineToPoint(context, rect.width / 4, rect.height / 4)
CGContextFillPath(context)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12155 次 |
| 最近记录: |