UIBezierPath未按预期更新 [英] UIBezierPath not updated as expected

查看:77
本文介绍了UIBezierPath未按预期更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于自定义视图的代码:

This is my code for my custom view:

class CircleView3: UIView {
    let endPoint = 270.0
    var index = 0.0
    var startPoint: Double {
        get{
            index++
            let div = 360.0/60.0
            let vaule = div * index
            return 290.0 + vaule
        }
    }

    func degreesToRadians (number: Double) -> CGFloat {
        return CGFloat(number) * CGFloat(M_PI) / 180.0
    }

    override func drawRect(rect: CGRect) {
          super.drawRect(rect)
        let startAngleRadiant: CGFloat = degreesToRadians(startPoint)
        let endAngleRadiant: CGFloat = degreesToRadians(endPoint)
        print("start = \(startAngleRadiant)")
        print("end = \(endAngleRadiant)")
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let path = UIBezierPath()
        path.moveToPoint(center)
        path.addArcWithCenter(center, radius: self.frame.height/2.2, startAngle: startAngleRadiant, endAngle: endAngleRadiant, clockwise: true)
        path.addLineToPoint(center)
        let strokeColor: UIColor = UIColor(red: 155.0/255.0, green: 137.0/255.0, blue: 22.0/255.0, alpha: 1.0)
        strokeColor.setFill()  //strokeColor.setStroke()
        path.fill()
        path.stroke()
    }
}

我正在画一个圆。我每秒从视图控制器中调用 setNeedsDisplay 函数。我应该画一个圆,它每秒减少其起点和终点,但即使更新后,圆仍保留其最后一行(位置)。再加上圆圈没有被我的颜色填充,只是填充了线条。

I am drawing a circle. I call the setNeedsDisplay function from a view controller each second. I am supposed to draw a circle that is reducing its start and end point each second, but the circle keep its last line (position) even after the update. Plus the circle is not filled with my color, just the line is filled.

http://www.mediafire.com/watch/xnkew5eu8da5wub/IMG_0066.MOV

我在 drawRect 中打印起始值,并且这些值正确:

I print the start values in drawRect, and the values are correct:

start  = 293.6
start  = 297.2
start  = 300.8
start  = 304.4
start  = 308.0
start  = 311.6
start  = 315.2
start  = 318.8
start  = 322.4
start  = 326.0
start  = 329.6
start  = 333.2
start  = 336.8
start  = 340.4

当然,这些是将其更改为弧度之前的值。

of course these are the values before changing them to radians.

在我调用 strokeColor.setFill()情况较好,但我仍然看到问题所在。请观看此新视频:

After I call the strokeColor.setFill() the situation is better, but I still see the problem. Please see this new video:

http://www.mediafire.com/watch/nb1b3v2zgqletwb/IMG_0069.MOV

推荐答案

,我将您的代码放入项目中,并且看到一个缩小的圆圈,因为在customView上调用了 setNeedsDisplay 。它不会像您的视频那样绘制。

For what its worth, I put your code in a project and I see a shrinking circle as setNeedsDisplay is called on the customView. It does not draw like your video.

这是我的附加代码:

class ViewController: UIViewController {

    @IBOutlet weak var circleView: CircleView3!

    override func viewDidLoad() {
        super.viewDidLoad()
        NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: "updateCircle", userInfo: nil, repeats: true)
    }

    func updateCircle() {
        circleView.setNeedsDisplay()
    }
}

这篇关于UIBezierPath未按预期更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆