I'm beginner with programming for OS X and I have problems with setting NSImageView transparency.
In iOS it was quite simple, there's property in UIImageView called alpha and I could change it and animate it. (like imageView.alpha = 0.5)
How can I achieve the same with NSImageView?
set the property alphaValue of a NSView that has a layer!
[view setWantsLayer:YES];
[view setAlphaValue:0];
Related
I know I can create border using below code.
[[myImageView layer] setBorderWidth:2.0f];
[[myImageView layer] setBorderColor:[UIColor greenColor].CGColor];
However this draw border inside image.
What I was looking is draw border outside ImageView.
Note:
I search online for this and found below.
Can be done by using another image which will have border.
Can be done by drawing another view which is little bigger then current image.
Is there quick way (especially in-built in iOS), where I can draw border outside UIImageView? Any views?
Why don't you try border with using imageview's frame ?
CGFloat borderWidth = 5.0f;
self.imgview.frame = CGRectInset(self.imgview. frame, -borderWidth, -borderWidth);
self.imgview. layer.borderColor = [UIColor blueColor].CGColor;
self.imgview. layer.borderWidth = borderWidth;
There is no quickway in-built in iOS, there is no margin that you could set on the image layer.
If I were you, I'd develop a new class that inherit from UIView (ex UIImageWithBorderView) and which include a UIImageView and making the "UIImageWithBorderView" bigger than the UIImageView (and think about NOT to autoresize the UIImageView with the UIView parent, otherwise your UIImageView will be stretched, and prevent the UIImageWithBorderView from being smaller than the UIImageView frame), and then add borders to the "UIImageWithBorderView".
This way, your UIImageView will be intact and you'll have a specific, reusable composant for your needs.
Hope it helps !
I have made a video player,and images are draw in a NSView.Have any method to convert NSView to CALayer?I try to use layer-hosting view,but developer document said can not add any subviews to layer-hosting view.Anyone can give me some suggestions?This code can run in OSX 10.6.8,but OSX 10.7 and 10.8.
mDisplayView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
mDisplayLayer = [[CALayer layer] retain];
[mDisplayView setLayer:mDisplayAudioLayer];
[mDisplayView setWantsLayer:YES];
[mDisplayView addSubview:mContentView];
[mRootLayer addSublayer:mDisplayAudioLayer];
The image of video had been drawn on mContentView.I just need find a way to make the mContentView into CALayer,is that possible?
You do not convert a view to a layer - you either draw into a view traditionally by overriding drawRect: or you switch it to layer-backed view and use CoreAnimation.
Best to start here to understand the basic concepts involved:
Core Animation Programming Guide
I'm trying to create a custom NSButton with a 50% opaque black background and white text. To do this I've subclassed NSButton and overloaded DrawRect:
- (void) drawRect:(NSRect)dirtyRect
{
[self setBordered:NO];
//REMED since it has same effect as NSRectFill below
//[[self cell] setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.2]];
NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:0 alpha:0.3f];
[backgroundColor setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
The white text appears fine but the button's background is always 100% opaque. The alpha value is not interpreted.
Any ideas? Thanks!
The default operation of NSRectFill() is copy which is not what you want. Replace it with
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop);
Another solution I found was to keep my code the same but turn on the Core Animation Layer for each button in Interface Builder. I don't know enough about Core Animation Layer to know why this worked. I had previously turned CAL off because it was making my fonts look very jagged.
I'm looking for a way to draw a fade effect on a table view (and outline view, but I think it will be the same) when the content is scrolled. Here is an example from the Fantastical app:
Also a video of a similar fade on QuickLook windows here.
To make this I tried subclassing the scrollview of a tableview with this code:
#define kFadeEffectHeight 15
#implementation FadingScrollView
- (void)drawRect: (NSRect)dirtyRect
{
[super drawRect: dirtyRect];
NSGradient* g = [[NSGradient alloc] initWithStartingColor: [NSColor blackColor] endingColor: [NSColor clearColor]];
NSRect topRect = self.bounds;
topRect.origin.y = self.bounds.size.height - kFadeEffectHeight;
topRect.size.height = kFadeEffectHeight;
NSRect botRect = self.bounds;
botRect.size.height = kFadeEffectHeight;
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeDestinationAtop];
// Tried every compositing operation and none worked. Please specify wich one I should use if you do it this way
[g drawInRect: topRect angle: 90];
[g drawInRect: botRect angle: 270];
[NSGraphicsContext restoreGraphicsState];
}
...but this didn't fade anything, probably because this is called before the actual table view is drawn. I have no idea on how to do this :(
By the way, both the tableview and the outlineview I want to have this effect are view-based, and the app is 10.7 only.
In Mac OS X (as your question is tagged), there are several gotchas that make this difficult. This especially true on Lion with elastic scrolling.
I've (just today) put together what I think is a better approach than working on the table or outline views directly: a custom NSScrollView subclass, which keeps two "fade views" tiled in the correct place atop its clip view. JLNFadingScrollView can be configured with the desired fade height and color and is free/open source on Github. Please respect the license and enjoy. :-)
I'm creating a custom NSSlider where I want to draw labels underneath each of the tick marks.
I'm currently doing this in the custom NSSliderCell -(NSRect)rectOfTickMarkAtIndex however because the height of NSSlider is fixed, the label I'm drawing underneath is being cropped.
Anyone have any ideas?
Also any resources with full implementations of custom NSSliders would be appreciated.
Simply set the frame and bounds of the NSSlider (which is really a subclass of NSView) so it is higher. Then your drawing should work fine. Stick this code in awakeFromNib: (Replace slider with self if you're in its subclass.)
NSRect frameRect = [slider frame];
frameRect.size.height = 30;
[slider setFrame:frameRect];
NSRect boundsRect = [slider bounds];
boundsRect.size.height = 30;
[slider setBounds:boundsRect];