简体   繁体   中英

How to add blinking circle inside draw:inRect: method of UIView?

Inside my custom UIView and draw:inRect initializer I have following code:

override func draw(_ rect: CGRect) {
    super.draw(rect)

    //...

    let axisPath = UIBezierPath()

    axisPath.move(to: CGPoint(x: paddingHorizontal + 20, y: paddingVertical))
    axisPath.addLine(to: CGPoint(x: leftOffset, y: bottomOffset))
    axisPath.addLine(to: CGPoint(x: rect.width - paddingHorizontal, y: bottomOffset))
    axisPath.lineWidth = 1

    UIColor.black.set()
    axisPath.stroke()

    var currentIndex = 0

    for yearData in data {

      //...

        let circle = UIBezierPath(roundedRect: CGRect(x: x - 4, y: y - 4, width: 8, height: 8), cornerRadius: 4)
        circle.fill()
        circle.stroke() //red circle, I need to make it blinking somehow
        //...
    }

    //...
}

The result is following:

在此处输入图片说明

Now I need to make red circle blinking:) How can I do that?

I would take a different approach. Use draw(_:) only for the static part of the graph.

The blinking red circle should be a simple little subview added over the graph view. Use a repeating UIView animation to fade the little circle view in and out using its alpha property.

See How do I get this fade animation to work? for an example of performing the blinking animation.

Setup a NSTimer and draw and undraw your red circle based on that. You have at least two choices: Create the red circle as an UIView and just set Hidden flag appropriately, or toggle a flag on your timer and invalidate the area encompassed by the red circle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM