Semi-transparent mesh background with opaque buttons - objective-c

I have read time and again that if you make a UIView transparent, it affects all its subviews, including buttons, etc. I want the background of my UIView to be semi-transparent (alpha=.5 perhaps) and to be made of a specific image pattern that repeats itself, but I want the buttons in that view to be completely opaque. I could separate the buttons into separate views, but that doesn't seem efficient. What is the best approach here? Regarding a mesh background, if I make a pattern I like, is this all that is required:
self.view.backgroundColor= [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"yourimage.jpg"]];

Why not open your image in an image editor and save it as a 50% transparent image?

Related

Completely transparent UIButton with irregular hit area

I know how to use a background image with alpha channel to create a UIButton with an irregular tap area. But with this solution, only the ignore-tap area is transparent; the tap area consists of the opaque.
What I want is a totally transparent UIButton, with an irregular tap area. (It triggers an animation behind the button.)
It seems that some sort of extra UILayer with some hit-testing could work, but I don't quite see how. Suggestions welcome.
Here's my solution. The problem: A UIImageView (call it the base view) to which I want to attach an irregularly-shaped tap area. The area corresponds to something in the image, let's call it a river. It could be a non-connected region.
In the Finder, duplicate the view's image, and make all the non-river pixels transparent, using your favorite graphics app. Call this the mask image.
When the base view is tapped, check to see if the corresponding pixel in the mask image is or is not transparent. (Solution left to reader; examples abound.)
So the mask image is never actually rendered, it is just used as a reference.
Note that if the base view gets resized or moved around (as it does in my app, via animating its constraints) then you have to "move" the mask image too, or in some other way manage the coordinate translation between view and mask. The easiest way to do this is to make the mask image a hidden subview of the base view, with its left, top, leading and trailing anchors constrained to be equal to those of the base view. Then they move around together, and the coord translation between the two is the identity.
I wrapped all this up in a subclass of UIImageView so that it manages its own mask, and calls a delegate if the "river" (or whatever) is tapped.

Transparent NSWindow but with standard border and shadow [duplicate]

This question already has an answer here:
Holes in NSView or NSWindow
(1 answer)
Closed 6 years ago.
I want to have a more or less standard NSWindow with a toolbar and all that, but I want the content view to be transparent so that I can see through it. At the same time I want to keep the light gray outline of the window and also it's shadow. BUT I want to avoid the "inner" shadow I get from the toolbar inside the content view area.
What I have tried so far is just to set the window background color to a semi transparent color and also set opaque to NO. The problem is that the window border fades away with the alpha of the background itself, and the more transparency I have on the background, the more the shadow of the toolbar shows up within the content view.
Generally, the window shadow and border changes depending on the transparency of the content view, which I totally understand. But I want a behavior where it keeps the border and shadow just as if it was a completely opaque window, and then I want the content view area to be transparent.
I am not sure what I need to do conceptually to make it work. Maybe I have to draw the window border myself, maybe not. Maybe I need to draw the shadow myself, or maybe not.
Is there anyone that know how to build this? I don't need exact code details, but rather what parts I need to do custom..
I appreciate any input!
I dont't know if this is of any value for you after all this time but try:
[aWindow setOpaque:NO];
[aWindow setBackgroundColor:[NSColor clearColor]];
Subclass the NSView class, override the drawRect:(NSRect)dirtyRect method and set the color of the view as clearcolor, now set the class of your content view as the Subclass of NSView.

Assigning a Background image to CGRect Object!

I am new to iphone Programming . I have a rectangle on which i display some data. I want to assign an image to its background. And is it possible that the data would be visible even after the image is assigned . Any idea how i can do it ?
Thanks
You don't say how you are displaying your data, so it isn't possible to respond properly.
But in general, you create simple displays using Interface Builder, and can position a UILabel over a UIImageView, and can set the labels background to be transparent.
If you are drawing using drawRect:, then you probably shouldn't be. And yes, you can draw text over an image doing it this way, too.
CGRect is just a structure representing an abstract rectangle. You do not associate styling information (e.g. color, background image, etc.) to it because this does not make sense.
If you simply want to display an image on a window, drag the PNG image into your project, then you can find your image in Interface Builder, which can be put onto the window.

Howto draw a rect on screen. NSOpenGLContext vs transparent NSWindow + custom NSView

I'm reading pixels from an area of the main screen via NSOpenGLContext. Now I would like to draw a rect around that area to indicate where it actually is.
How would I do this?
My first thought was the "Cocoa way": create a transparent fullscreen NSWindow and a custom NSView to draw the rectangle path. But that feels a bit too complicated. Isn't it possible to draw directly on the NSOpenGLContext?
If you want to draw over elements not inside your application the floating window is the only correct way. There’s really no complication except mapping positions properly, which is easy to do with the coordinate-space conversions available on NSView and NSWindow.

setOpaque:NO vs setBackgroundColor:[NSColor clearColor]

I'm going through a tutorial on drawing a custom [shaped] window with cocoa by subclassing NSWindow.
The tutorial states that in initializer developer should do the following:
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
So i'm wondering what is the differnce between these two messages and why are they needed both since their result is the same.
References: tutorial can be found here.
I guess that the first message is required because the drawing system needs to know whether it should bother updating views that lie behind yours. For example, if a window in another application (behind your window) updates (say text appears etc) the windowing system would not normally need to redraw it, but since your window is transparent it does in this case.
So i'm wondering what is the differnce between these two messages and why are they needed both since their result is the same.
They're not the same.
Look at the documentation for the opaque property: It's how you tell NSView that you're going to draw in your entire bounds, completely covering anything below your view.
If you don't cover the entire bounds, or you don't always draw at 100% opacity, then your view is not opaque, and you should leave that property set to NO.
If you set your view's background color to clearColor (which is simply a color with 0% opacity), and don't draw at 100% opacity over the entire background, then your view is not opaque.
On the other hand, it is possible to have clearColor as your background and then completely draw over it, in which case your view is opaque and should set itself as such.