OS X version of bringSubviewToFront:? - objective-c

I need to replicate the function of bringSubviewToFront: on the iPhone, but I am programming on the Mac. How can this be done?

Haven't actually tried this out - and there may be better ways to do it - but this should work:
NSView* superview = [view superview];
[view removeFromSuperview];
[superview addSubview:view];
This will move 'view' to the front of its siblings

Pete Rossi's answer didn't work for me because I needed to pop the the view to front when dragging it with the mouse. However, along the same lines the following did work without killing the mouse:
CALayer* superlayer = [[view layer] superlayer];
[[view layer] removeFromSuperlayer];
[superlayer addSublayer:[view layer]];
Also, the following placed in a NSView subclass or category is pretty handy:
- (void) bringToFront {
CALayer* superlayer = [[self layer] superlayer];
[[self layer] removeFromSuperlayer];
[superlayer addSublayer:[self layer]];
}

also be sure to enable the layer for quartz rendering:
...
NSImageView *anImage = [[NSImageView alloc] initWithFrame:NSRectMake(0,0,512,512)];
[anImageView setWantsLayer:YES];
...
otherwise, your layer cannot be rendered correctly.

Pete Rossi's answer works, but remember to retain the view when you remove it from the superview.
You can add this in a category on NSView :
-(void)bringSubviewToFront:(NSView*)view
{
[view retain];
[view removeFromSuperview];
[self addSubview:view];
[view release];
}

Sibling views that overlap can be hard to make work right in AppKitā€”it was completely unsupported for a long time. Consider making them CALayers instead. As a bonus, you may be able to reuse this code in your iOS version.

This is Swift 3.0 solution:
extension NSView {
public func bringToFront() {
let superlayer = self.layer?.superlayer
self.layer?.removeFromSuperlayer()
superlayer?.addSublayer(self.layer!)
}
}

Related

NSLayoutConstraint behaving differently when drawing to NSBitmapImageRep

I have this control called ITNavigationView on Github.
It's smoothly animating from one NSView to another by caching and adding them to a NSImageView.
When caching the view, a subview centred in the x axis will be pulled exactly 1 pixel to the right.
If I instead add a leading constraint, this doesn't happen.
How can I prevent this from happening?
To cache the view, I'm using this code:
- (NSImage *)imageOfView:(NSView *)view {
[view layoutSubtreeIfNeeded];
[view setNeedsUpdateConstraints:YES];
[view updateConstraintsForSubtreeIfNeeded];
NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:view.bounds];
[view cacheDisplayInRect:view.bounds toBitmapImageRep:rep];
return [[NSImage alloc] initWithCGImage:[rep CGImage] size:view.bounds.size];
}
EDIT
Also worth noting is that this only happens when the superview has an odd width.

UITapGesture only works after reLoad UIViewController

I have 1 year works with Objective-C and is my first question that I make because I always found the answer in this site, but this situation is completely insane and did not found anything similar.
I have a custom class (called BallClass from UIView). The UIView have an UIImageView inside on it and a UITapGesture to make an action.
So when I create the object in a UIViewController, does not works the tapAction when press in the object, but if I change to other screen and return it, the tapGesture works perfect.
I tried many options, but I cant makes work in the first load.
I work with ARC, Xcode (4.6.3) and OSX (10.8.4) and IB but this objects was created with no IB .
Thanks in advance.
Here is the class BallClass.m
- (void)tapGesture:(UIGestureRecognizer *)tapGesture {
NSLog(#"hola");
}
- (id)initBall:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
// Recibir las variables iniciales para el control del timer
ball = [[Ball alloc] init];
// Agregar la imagen de la pelota en el view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
[imageView setCenter:CGPointMake(CGRectGetMidX([self bounds]), CGRectGetMidY([self bounds]))];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:#"pelota.png"]];
[imageView setTag:100];
[imageView setNeedsDisplay];
// Add TAP
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
// Tap Properties
[tapGesture setDelaysTouchesBegan:YES];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture setDelegate:(id)self];
[imageView addGestureRecognizer:tapGesture];
[self addSubview:imageView];
[self setNeedsDisplay];
// Propiedades del View
[self setUserInteractionEnabled:YES];
self.backgroundColor = [UIColor clearColor];
[self setTag:100];
// BALL BOUNCE
[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
}
return self;
}
- (void)onTimer:(NSTimer *)timerLocal {
// Do something
}
... // more methods
Here is the instantiate:
ballView00 = [[BallView alloc] initBall:CGRectMake(10, 10, 40, 40)];
The UiView show perfect, and the animation works fine, but the UITapGesture just works, as mentioned early, after reLoad the ViewController.
Here is the link to image the Ball and UIView (#kBall). Sorry, about image, but I cant load until I have 10 points :(
This might be an issue with the superview's frame. What view is holding ballView00? Does ballView lie entirely within it's superview's bounds?
Usually when you encounter problem where you're expecting a touch but don't get it it's because the view set to receive the touch is outside one of its parents' bounds.
So if the superview bounds are 100x100 and the ball is at x=125 y=125 it will not receive a touch.
A lot of magic happens when your views get laid out, it's not surprising to sometimes find one with 0x0 dimensions because of some incorrect constraints.
You can easily check the bounds of all your views by giving them a background color.
I found the solution.
The ball make a spin with this:
[UIView animateWithDuration: 0.5f
delay: 0.0f
options: UIViewAnimationOptionCurveEaseIn
animations: ^{
self.transform = CGAffineTransformRotate(self.transform, M_PI / 2);
}
completion: ^(BOOL finished) {
if (finished) {
if (animating) {
// if flag still set, keep spinning with constant speed
[self spinWithOptions: UIViewAnimationOptionCurveLinear];
}
}
}];
And generate conflict with the main timer. I change the spin action with simple add self.transform = CGAffineTransformRotate(self.transform, M_PI / 2); inside drawRect method in the class.
Apparently, I can't nested two timers, because "disable" the tapGesture, so when I change of the screen and return, the spin was stopped and this help to found this situation.
Thanks for your help.

Change cursor for full screen NSWindow

I am trying to make an overlay window that will allow drawing at the ShieldingWindowLevel, however when the window appears the cursor is still the default pointer. I would like to change it to the crosshairs. Having controller NSCursors before I am baffled why resetCursorRects is not ever called.
I manually create the window as follows (in my AppController class):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Create the window
NSRect frame = [[NSScreen mainScreen] frame];
// Provide a small area on the right to move the cursor in-and-out of the window.
frame.size.width = frame.size.width - 20;
self.window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[self.window setAcceptsMouseMovedEvents:YES];
[self.window setOpaque:NO];
[self.window setLevel:CGShieldingWindowLevel()];
[self.window setBackgroundColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:0.2]];
// Create the subview
ScreenOverlayView *subview = [[ScreenOverlayView alloc] initWithFrame:NSZeroRect];
[[self.window contentView] addSubview:subview];
// Add subview and show window
[self.window setContentView:subview];
[self.window makeFirstResponder:subview];
[self.window orderFrontRegardless];
}
With the following NSView subclass:
#implementation ScreenOverlayView
- (void) resetCursorRects {
[super resetCursorRects];
[self addCursorRect: [self bounds]
cursor: [NSCursor crosshairCursor]];
}
// ...
#end
I created a sample project to show this case and posted it to github, the most interesting files are ScreenOverlayView.m and AppDelegate.m.
I should point out that I have also spent a good deal of time trying to get this working with an NSTrackingArea, as you can see in the sample project. Tracking Area works if the mouse enters the view after it has appeared, but not if it is inside to start with. Using MouseEnter and MouseLeave would be fine if I had some way to set the initial cursor, but it will only change for a split second before changing back.
How can I get resetCursorRects to be invoked -OR- how can I set the cursor when I move it to the superview?
The key is that you really need to create a custom subclass of NSWindow, in order to counteract some of the default behavior that borderless windows (NSBorderlessWindowMask) have.
An updated version of your sample project is at http://www.markdouma.com/developer/full-screen-overlay.zip.
In it, I created a custom MDScreenOverlayWindow class that overrides NSWindow's canBecomeKeyWindow method like below:
// Windows created with NSBorderlessWindowMask normally can't be key,
but we want ours to be
- (BOOL)canBecomeKeyWindow {
return YES;
}
This will allow your view to become key and basically all your other stuff to work properly.
The other thing that may be of note is the drawRect: method. (It looks like you may be coming from iOS). You might want to look into NSBezierPath, as it could potentially simplify some of your drawing code. For example, I believe the drawing code you had could be consolidated into the following:
- (void)drawRect:(NSRect)rect {
// the color should probably be "pre-multiplied" by the alpha
// premultiplied version:
[[NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.8] set];
[NSBezierPath setDefaultLineWidth:2.0];
[NSBezierPath strokeLineFromPoint:currentLocation toPoint:downLocation];
}

Cocoa - Animate a NSWindow with setFrame

I have the following block of code.
NSRect windowFrame = [window frame];
windowFrame.size.height = [view frame].size.height + WINDOW_TOOLBAR_HEIGHT;
windowFrame.size.width = [view frame].size.width;
windowFrame.origin.y = NSMaxY([window frame]) - ([view frame].size.height + WINDOW_TOOLBAR_HEIGHT);
if ([[contentView subviews] count] != 0)
{
[[[contentView subviews] objectAtIndex:0] removeFromSuperview];
}
[window setFrame:windowFrame display:YES animate:YES];
[contentView setFrame:[view frame]];
[contentView addSubview:view];
It switches the frame to the new view but I never get a shrinking/growing animation. If I place this before the [window]
NSLog([NSString stringWithFormat: #"%.2f", windowFrame.size.height]);
I get different sizes.. here is the output
2011-09-07 14:13:02.418 Spark[29919:903] 492.00
2011-09-07 14:13:03.610 Spark[29919:903] 580.00
2011-09-07 14:13:05.955 Spark[29919:903] 492.00
2011-09-07 14:13:08.339 Spark[29919:903] 255.00
Not sure what I am missing.
thanks!
EDIT
I've added a simple project to see my issues
http://dl.dropbox.com/u/13731121/Spark.zip
I've reviewed your project and here are my points:
Your preference.xib is... non standard to say the least. Usually when creating window controller, you should set window controller class as window nib's file owner. Your preference.xib file owner is NSObject while PreferenceController is separate object. So I've fixed that and rebound almost all IBOutlets.
I've rebound almost all outlets because you don't need such thing as contentView, because window itself has such property as contentView, hence it's enough to set the contentView property of the window.
You set contentView's FRAME which is very, very wrong (unless you really know what you're doing). I've changed it to set contentView's BOUNDS instead.

remove every type of subview from uiscrollview?

hello all i am using this code to show flip animation...... i have a uiview with scrollview(paging enabled)...so it shows a view like a page...now i also have done flip animation using this code....
-(void)flipView
{
flashCardAnswerController *flashCardAnswerControllerobj = [[flashCardAnswerController alloc] initWithNibName:#"FlashCardAnswerView" bundle:[NSBundle mainBundle]];
[flashCardAnswerControllerobj.view setFrame:[[self view] frame]];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.60];// Sub. duration
UIView *superview;
if ((superview = [[self view] superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superview cache:YES];
[superview addSubview:[flashCardAnswerControllerobj view]];
}
[UIView commitAnimations];
}
so according to code the new view will be added to scrollview..i also manage to release the new view while flipping back...but if i directly push the back button on navigaion controller...the flip side view won't release. i tried this in dealloc method of my scrolleview's class but it doesn't work
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
how do i remove flipside's view the views are very much in number...approx. 48 plus 48 flip side views so definietly it will create memory issues on device...
also could someone tell me a way to disable NavigationBar's back button in flip side view...without creating scolling class's object.
I do not have a direct answer to your question, but I can tell that this is the old iPhone OS 2.x way of displaying a views flip-side.
In iPhone OS 3.0 you should use presentModalViewController:animated: instead. This is much less code, and all of the problems you describe in your questions goes away.
The idea is that the flip-side of the current view, is managed by a separate UIViewController that you display modally. But in addition to the sliding animation from 2.0 you can also have the new view controller animate in and out with a horizontal flip.
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:flipSideController
animated:YES];
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)]