Re: CGContext always creating a black rect.


Alex Zavatone
 

Thanks.

I know I don’t need the self, but I want the context.  A variable just sitting around tells me nothing about the context in which it exists.  I want to see the context and want to see the self.

- The `roundRect(…)` function would be better named as `drawRoundRect(…)`.

Good point.

On Jul 24, 2021, at 2:32 PM, Ben Kennedy <ben-groups@...> wrote:

On 24 Jul 2021, at 11:37 am, Alex Zavatone via groups.io <zav@...> wrote:

Hi.  I’m trying to draw a bezier shape in CGContext on a UIView in Swift with a transparent background and the background is always black.  Nothing online helps.

Any ideas?  I’ve checked .isOpaque, backgroundColor.   Nothing I can do to get rid of the black rect background.  Any ideas?

Move your `backgroundColor` and `isOpaque` calls to the init method(s). I set up a test project with your code, and that solves the problem.

I was going to make that suggestion before I even tested it, though, because it's an obvious smell to me: there's no need to repeatedly set those properties every time you draw, but rather just once, at setup.

A couple of other code style comments:

- You don't need all the `self.` prefixes.

Want them.  They indicate scope.  I don’t need to think.  I just look at it and know the scope.  Anything that makes code more vague sucks.  I want easier understanding rather than “but we can use less words!”  People’s time is $$.

- The `roundRect(…)` function would be better named as `drawRoundRect(…)`.

- That same function seems like it ought to be pure; i.e., have no side effects -- just draw a rect based on its argument. However, it does math and sets instance variables. If it needs to affect and act on persistent state, it ought to be refactored.

Refactored how?

It can’t exist without its configuration being set and it will be drawing with those internal settings once I have the details set up.  Then it’s possible that I’ll move the configuration internal.  Eventually, it will have a gradient interior.  It’s replacing a UISwitch.

Here’s how I currently call it.

        self.roundedRect = RoundedRectUIView()
        self.roundedRect.backgroundColor = .clear
        self.roundedRect.clipsToBounds = true
        let rectBorderWidth = CGFloat(2)
        let rectWidth = CGFloat(100)
        let rectHeight = CGFloat(40)
        let rectBorderColor = UIColor.blue
        let rectBgColor = UIColor.systemGray3
        let rectCornerRadius = CGFloat(-1)
        let origin = CGPoint(x: 10, y: 200)
        viewRect = CGRect(origin: origin,
                          size: CGSize(width: rectWidth + rectBorderWidth * 2.0,
                                       height: rectHeight + (rectBorderWidth * 2.0)))
        self.roundedRect.frame = viewRect

                          

        self.roundedRect.configure(rectWidth: rectWidth, rectHeight: rectHeight, rectBgColor: rectBgColor , rectBorderColor: rectBorderColor, rectBorderWidth: rectBorderWidth, rectCornerRadius: rectCornerRadius)

        self.view.addSubview(roundedRect)

In Swift, I HATE how they have done method parameters, so I’m opting for redundant redundancy over terseness or brevity.

Thanks, Ben.
-ben







Join cocoa@apple-dev.groups.io to automatically receive all group messages.