How do I know if a view is visible or not? - objective-c

Say I have two view controllers: xVC and yVC. I have used the shake API and and have used the methods -(void)motionBegan,-(void)motionEnded: and -(void)motionCancelled in xVC. What happens is when the device is shaken, it fires a simple animation. Now the thing is that this animation is fired even when the I have yVC open that is, when yVS.view has been added as the subview. What I am looking for is some if condition which I can use in -(void)motionEnded: like this:
if(yVC == nil)
{
//trigger animation
}
By that I mean that the shake shouldn't work when yVC is visible. How do I do that? Please help.

The general advice I have seen and used is to ask a view if it has a non-nil window property:
if( ! yVC.view.window) {
// trigger animation
}
But note that this doesn't always equate with being visible; though in most apps it's about as good as you can performantly get (the basic case where it's not accurate is when a different view completely obscures it, but this may still satisfy your needs)

Add this to both of your view controllers:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
visible = YES;
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
visible = NO;
}
Now, just check the variable isVisible of both the view controllers and trigger your animation likewise.

The previous answers all work to some degree, but fail to take modally presented view controllers into account. If view controller A presents view controller B, of the previous answers will tell you that A is still visible. If you, like me, want to know whether or not the view is actually visible (and not just a part of the view hierarchy), I would suggest also checking the presentedViewController property:
if (self.isViewLoaded && [self.view window] && !self.presentedViewController) {
// User is looking at this view and nothing else
}
This works since presentedViewController will be non-nil whenever the current view controller OR any of its ancestors are currently presenting another view controller.

Related

How can I disable the touch events on two UIImageViews after i drag them into each other?

I have two draggable UIImageViews within a UIView. When i drag one UIImageView over the other, i would like to disable touch events to those two UIImageViews and create another UIImageView which does respond to my touch events. Ive tried using setUserInteractionEnabled: but it isnt really doing anything at all. Forgive me if this is a no brainer but i am new to programming, here is what i have so far. Please give me some feedback on my code and give me some constructive criticism because i feel as though i am setting this up all wrong.
-(void)swapImageViews
{
if ((self.imgView1.center.x == self.imgView2.center.x) &&
(self.imgView1.center.y == self.imgView2.center.y)) {
[self addSubview:self.imgView3];
self.imgView3.center = CGPointMake(self.imgView1.center.x, self.imgView1.center.y);
[self.imgView1 removeFromSuperView];
[self.imgView2 removeFromSuperView];
[self.imgView1 setUserInteractionEnabled:NO];
[self.imgView2 setUserInteractionEnabled:NO];
}
}
So once again, the goal is to swap out two image views with a fresh one that i can also drag around.
The problem i am running into is that my touch events are still moving the first two image views around behind the third one.
You say that the third image appeared, but the the other two are still in the view and they still receive touch events. There are two two scenarios for this:
You've already added the third image view somewhere else, and your method is never called, or the condition is never met.
self.imgView1 and self.imgView2 are not set to the actual objects in your view stack. They're probably just nil. you can debug that by NSLog(#"%# %#", self.imgView1, self.imgView2); in that method, before the condition, or in viewWillAppear or viewDidLoad.
The reasons that support my opinion:
In that method, you call removeFromSuperview to the two image views you want to disable, yet they are still visible and receiving touch events. That means that this method is not executed. Which means they're nil or some other objects, that are different than the visible ones.
The condition in the start of the method is a bit strict. It's hard to be easily met. Yet the method is executed as the third image view is added to the view. This can happen when the object is nil, so the values returned by methods are the default values.
Check this link "the 10th item" and this one too.
Well this is not the best way to do that, but with the code that you provide to us, we have not a lot of options, you can do something like that, to remove the gestures:
-(void)swapImageViews{
if ((self.imgView1.center.x == self.imgView2.center.x) &&
(self.imgView1.center.y == self.imgView2.center.y)) {
[self addSubview:self.imgView3];
self.imgView3.center = CGPointMake(self.imgView1.center.x, self.imgView1.center.y);
[self.imgView1 removeFromSuperView];
[self.imgView2 removeFromSuperView];
NSArray* gestures1 = [self.imgView1.gestureRecognizers copy];
for(UIGestureRecognizer *gesture in gestures1){
[self.imgView1 removeGestureRecognizer:gesture];
}
NSArray* gestures2 = [self.imgView2.gestureRecognizers copy];
for(UIGestureRecognizer *gesture in gestures2){
[self.imgView2 removeGestureRecognizer:gesture];
}
}
}

Getting this warning "while a presentation is in progress" (Xcode)

In a project I'm writing I get this error when I present a new view controller:
Attempt to present.... while a presentation is in progress!
I think it happens because I first present a new view controller, and then in that view I present another view controller.
- (void)loadLabelSettings {
LabelSettingsViewController *labelSettings =
[[LabelSettingsViewController alloc] init];
labelSettings.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:labelSettings animated:YES completion:nil];
}
The program doesn't crash or anything it runs just fine, and there is no errors or warnings in my code. So my question is: Is it something I should be concerned with and if yes how do I solve it?
Thanks in advance :)
It is, like you said, probably caused by presenting two view controllers at the same time. Wait with presenting the second view controller until the first one has been fully presented. A good location would be to do this in viewDidAppear.
In my case, I connected a UIViewControllers UIButton with a second UIViewController by a UIStoryboardSegue. Inside my code a called it a second time programmatically. So pressing the UIButton caused presenting the specified view two times.
I figured out my problem, as Scott wrote it was because I was presenting 2 view controllers at the same time. It happened because I had a button that had a UILongPressGestureRecognizer, that showed the new view controller. The problem was that when using a UILongPressGestureRecognizer, the method that is being called, is called twice. First when the long press is detected and when your finger is released from the screen. So the presentViewController method of the same view, was called twice. I fixed this by only reacting to the first detection. Here is the code :
- (void)loadButtonSettings:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
}
}

Can I use a customized checkmark with UITableView's allowsMultipleSelectionDuringEditing set to YES?

A picture's worth a thousand words...
For a bit more background, I have a UITableView leveraging iOS 5's allowsMultipleSelectionDuringEditing set to YES. This results in the empty and filled edit controls being shown on the left of the cell any time the cell is in edit mode. This behavior is exactly what I want. I just want to change the appearance of these check marks.
I know it would be possible to write custom selection logic and basically roll my own version (like this and this), but that's what I want to avoid. The system is already in place, and I want to re-use as much of it as possible.
This is the closest I've come. It's simple and it works, while reusing almost all of the pre-baked system. It's also a giant hack however, and relies on exploiting the undocumented view hierarchy of UITableViewCell after a little runtime introspection.
In a nutshell, this simply hides the view normally responsible for showing the checkmark, allowing me to add my own view that can be shown in its place. I can then manipulate this stand-in view when the cell's selection or editing state changes...
To prevent the standard checkmark from appearing, all that's needed is a custom -layoutSubviews implementation. It's called, per the documentation, after both -willTransitionToState: and -setEditing:animated:, ensuring the state is always valid when either isSelected or isEditing changes.
- (void)layoutSubviews
{
[super layoutSubviews];
// Find the offending view, and quietly bury it...
for (UIView* subview in [self subviews])
{
// As determined by NSLogging every subview's class, and guessing which was the one I wanted
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellEditControl"])
{
[subview setHidden:YES];
}
}
if ([self isEditing])
{
// Show the custom view however you want.
// The value of [self isSelected] will be useful...
}
else
{
// Hide the custom view.
}
}
I would still welcome a solution that's a bit more... kosher.

Get current view

I have an app which has split view inside a tab bar, and these split views often have navigation hierarchy and then sometimes modal views are presents on top of them, and it all works fine, but...
I am trying to display a passcode lock whenever the app goes into background, so I put
[self.window.rootViewController presentModalViewController:lockView animated:YES];
in my AppDelegate's method
- (void)applicationWillResignActive:(UIApplication *)application
...which works fine unless a modal view is displayed.
the passcode does not display if a modal view is open.
Is there a way to retrieve the currently active view controller so I can present this lock view?
Thanks in advance
Cheerio
Code that worked was as follows:
BOOL hasKids = YES;
UIViewController *topViewController = (UIViewController*)[[(UITabBarController*)self.window.rootViewController viewControllers] objectAtIndex:((UITabBarController*)self.window.rootViewController).selectedIndex];
while (hasKids) {
if (topViewController.presentedViewController) {
hasKids = YES;
topViewController = topViewController.presentedViewController;
} else {
hasKids = NO;
}
}
[topViewController presentModalViewController:lockView animated:YES];`
I think the easiest way is to keep track of which tab is currently active (there are a number of ways to do this, but I'd recommend implementing the UITabBarControllerDelegate and handling its tabBarController:didSelectViewController: method).
Once that's done, you'll probably need to manage a property in each view controller that holds any modal view controllers you present. If, however, you're on iOS 5 or higher, look into the UIViewController property presentedViewController. It appears that this is a new way to do exactly what you want.

Proper way to determine if NSView is drawn

Is there a proper way to determine if a NSView is actually drawn in the current view hierarchy or not, considering cases like:
The view is completely offscreen (not mandatory)
The view is not on top of the view hierarchy
The -isHidden and -isHiddenOrHasHiddenAncestor are unfortunately not set when e.g. a view disappears because a tab view switches to another tab.
The reason for this is that I have an attached child window and I would like to be able to hide it as well when the view that it is attached to is not drawn.
I have found a trick to tell if it is visible, but it requires subclassing. It works by toggling an ivar on 2 events.
- (void)discardCursorRects {
isDrawn_ = NO;
[super discardCursorRects];
}
- (void)resetCursorRects {
isDrawn_ = YES;
[super resetCursorRects];
}
Whether (or when) it's drawn is supposed to be "none of your business" and really have nothing to do with whether or not it's on-screen. Use NSView's -viewDidMoveToSuperview or -viewDidMoveToWindow to manage this.