gpt4 book ai didi

ios - 未能对 UIBezierPath 进行描边()

转载 作者:行者123 更新时间:2023-11-29 02:40:49 26 4
gpt4 key购买 nike

我希望创建一个完美的圆形矩形(圆形)并将其绘制在屏幕上。我已经在 Playground 上测试了我的代码,它成功地绘制了 UIBezierPath。然而,它并没有在 iOS 模拟器中成功绘制它。这是我一直在处理的代码:

    class Circles {

//Defining the rounded rect (Circle)
func roundRect(radius: CGFloat, angle: CGFloat) -> UIBezierPath {

//Creating the rounded the rectangle (Circle)
var roundedRect = UIBezierPath()
roundedRect.addArcWithCenter(CGPointZero, radius: radius,
startAngle: 0, endAngle: angle ,
clockwise: true)

return roundedRect
}


//Drawing the Bezier Path (Circle)
func drawRect(rect: UIBezierPath){

rect.moveToPoint(self.point)
UIColor.blackColor().setStroke()
rect.stroke()
}

//Giving the rounded rect (Circle) it's position
var point = CGPointMake(500, 500)
}
//Giving the rounded rect (Circle) it's size
var newRect = Circles().roundRect(200.0, angle: 7)

//Drawing the rounded rect (Circle)
Circles().drawRect(newRect)

几年前我看过其他一些有类似问题的帖子,但是它们是在 Objective-C 中,我尝试翻译但没有任何用处。我还尝试了其他几种在屏幕上绘制路径的方法,但遗憾的是,它没有用。我对它进行了测试以确保函数与 println 语句一起使用,问题是我不知道为什么笔画没有激活。感谢阅读,-Zach。

这是使用 Mike 所说的更新版本:

class CircleView: UIView {
override func drawRect(rect: CGRect) {

// Creating the rectangle's size
var newRect = Circles().roundRect(200.0, angle: 7)

//Drawing the rectangle
Circles().drawRect(newRect)
}


//Holding all to do with the circle
class Circles {

//Defining the rounded rect (Circle)
func roundRect(radius: CGFloat, angle: CGFloat) -> UIBezierPath {

//Creating the rounded rect (Circle)
var roundedRect = UIBezierPath()
roundedRect.addArcWithCenter(CGPointZero, radius: radius,
startAngle: 0, endAngle: angle ,
clockwise: true)

return roundedRect
}


//Drawing the Bezier Path (Circle)
func drawRect(rect: UIBezierPath){

rect.moveToPoint(self.point)
UIColor.blackColor().setStroke()
UIColor.blackColor().setFill()
rect.stroke()
rect.fill()
}

//Giving the rounded rect (Circle) it's position
var point = CGPointMake(500, 500)
}

}



class ViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//Generating the background
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "normalpaper.jpg"))

let circleView = CircleView(frame: self.view.bounds)
self.view.addSubview(circleView)

}

最佳答案

正如您在评论中提到的,您正在从 UIViewControllerviewDidLoad 函数中调用 drawRect。您那里没有有效的绘图上下文,所以这不会起作用。

最简单的方法是创建一个 UIView 子类,它有自己的 drawRect 函数,调用 Circle().drawRect(...) 并将其添加为 UIViewControllerview 的 subview 。

这是一个例子:

注意:我通过设置 circleView.opaque = false 使自定义 View 透明,这样您在评论中提到的背景就会显示出来。

class CircleView: UIView {
override func drawRect(rect: CGRect) {
// I just copied this directly from the original question
var newRect = Circles().roundRect(200.0, angle: 7)
Circles().drawRect(newRect)
}
}

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Create a new CircleView that's the same size of this
// view controller's bounds and add it to the view hierarchy
let circleView = CircleView(frame: self.view.bounds)
circleView.opaque = false
self.view.addSubview(circleView)
}
}

注意:如果您要进行自定义绘图,我强烈建议您阅读 Drawing and Printing Guide for iOS .它会教您让它发挥作用所需的所有基础知识。

关于ios - 未能对 UIBezierPath 进行描边(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com