sl0anage
sl0anage

Reputation: 97

Access the UIBezierPath drawn on a UIView

I have drawn a custom shaped UIView where I overwrote the drawRect(_:) method to be a triangle using this answer: The Stackoverflow answer

Now I want to highlight that triangle when the user taps it. I suppose I could draw another triangle on top of it with a different color, but I want this view to perform other tasks later. The custom view's bounds overlaps another view, so I can't just change the whole UIView's background color, only the UIBezierPath's background color. If you look at the attached image, the selected view's background is covering the nearby view. How can I access just the UIBezierPath's layer and change it's color?

The overlapping background is the top right view.

Upvotes: 1

Views: 791

Answers (1)

matt
matt

Reputation: 534950

Give the view a tap gesture recognizer. Now you know when it is tapped. You can use hit-testing and your knowledge of the shape to know whether the tap is within the triangle.

How can I access just the UIBezierPath's layer and change it's color?

If you had used the CAShapeLayer approach, you could have done that; all you would have to do is change the layer's fillColor (if that is what highlighting means to you).

But since you elected to use drawRect:, there is no "UIBezierPath's layer". You will have to call setNeedsDisplay, thus causing drawRect: to be called again — and this time, you draw the triangle filled (or whatever highlighting means to you).

Upvotes: 1

Related Questions