Drag-n-Drop from UIPopoverController to other UIView - iphone

How would I go about implementing dragging and dropping a UIView from UIPopoverController into the back UIView.
This is the functionality that Pages provide in their insert media popover, where you can drag a shape out from the UIPopoverController and drop it into the main document.
I am actually confused with the pan UIGestureRecognizers and where they will be implemented.
Thanks,
Umer

According to the documentation on UIPopoverController, when the popover is presented, it is presented on a special "window". Because of this, simply adding a subview to the popover view controller's content view controller is not sufficient to be able to drag a view outside of the popover view controller's view.
The easiest solution here is to create your own window, add your drag-able view to the window when dragging occurs. Make the window visible for the duration of the drag/drop, and then release your window when complete.
As mentioned above, gesture recognizers (GR) are best suited for Drag/Drop functionality. Once the GR's state has changed to "Began" the GR will control all touches until the "Ended" or "Cancelled" state is achieved which makes it ideal for dragging views between view controllers as well as windows :)
Example:
#interface MySplitViewController : UISplitViewController {
UIView *dragView;
UIWindow *dragWindow;
}
Implementation:
NOTE we do not need to call "makeKeyAndVisible" on our window. We just need to set its "Hidden" property
From Apple in regards to the makeKeyAndVisible method:
// convenience. most apps call this to show the main window and also make it key. otherwise use view hidden property
-(void)dragBegan{
self.dragWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
[self.dragWindow addSubview:self.dragView];
[self.dragWindow setHidden:NO];
}
Here we handle the Gesture Recognizer's "Ended" or "Cancelled" state.
NOTE: It is important to remove the window when the Drag/Drop is complete or you will lose user interactiveness with the views below.
-(void)dragEnded{
[self.dragView removeFromSuperview];
[self.dragWindow setHidden:YES];
[self.dragWindow release];
[self.view addSubview:self.dragView];
}

You have to deal with two view controllers one that's in the background called mainController one that presented using a UIPopoverViewController called popoverController. Your popoverController could add a UIPanGestureRecognizer to the views, that the user can drag. The action target of the gestureRecognizer could be a method on the popoverController.
Once the user starts a dragging operation your action method is called with the gestureRecognizer as an argument, were the state of the gestureRecognizer is UIGestureRecognizerStateBegan. You could than save the current frame of the view somewere to be able to animate it back, when the dropping fails. It might be necessary to move the view to an other superview (the window for example), because I'm not sure if UIPopoverViewController clipsToBounds its view.
As the user draggs, your action method is called over and over with the gestureRecognizer in the state UIGestureRecognizerStateChanged. Use the translationInView: method on UIPanGestureRecognizer to determine how much the user dragged and update the dragged views center/frame/transform accordingly.
Once the user lifts his finger the action method is called for a last time with the gestureRecoginzers state set to UIGestureRecognizerStateEnded. Now it's time to find out if the drag was successful. For example the popoverController could ask the mainController via delegation if there's a drop target under the views current position, if so the mainController can take action, else the popoverController would animate the dragged view back to were it came from, and add it back as a subview to it's view.
I hope this is somehow comprehensible and helpful.

Related

Creating a View popup over another ViewController on iPhone

I am trying to make some kind of popup view when a button i pressed on the iPhone. And it would be nice if I could manage that popup view with a ViewController. I have found out that the UIPopoverController could have been the solution, but it seems that it only works on the iPad...
But anyway, are there any similar solutions for the iPhone?
I am using storyboard
Check out these repos on Github:
https://github.com/werner77/WEPopover
https://github.com/50pixels/FPPopover
Create a separate view controller and resize its xib file and make it look like a popup.
Then ADD this view controller as a subview, and also add it as childController too.
[self addChildViewController:self.popOverViewController];
[self.view addSubview:self.popOverViewController.view];
Now Make it hidden initially.
self.popOverViewController.view.hidden = YES;
If a user taps on Button then using fade in & Fade out animation you can hide/unhide it.
I can tell you how to fade in and fade out if you want to know it further, I hope you can do it easily.
In interface builder make a UIView size of the screen and then make another in that Uiview with the style, size and the such for your pop over. Make its class, hook everything together.
CustomPopUpView *view = [[CustomPopUpView alloc] initWithFrame.....]
Add this all to your UIViewController with
[self.view addsubview:view]
Then attach a tapGestureRecognizer to the back view that animates the whole view off screen when tapped. So now if they click off your pop over view it close it will animates it off screen.
Hope this makes sense.
BooRanger

How to controll second UIView (subview) from a different UIViewController file in one XIB

I am strugling on the following task.
I am trying to control my subview from another viewController class.
What I did and does not work is this.
I inserted an object and changed it class to my second viewController class.
Then I connected its UIButton outlet to a button I have on my subview.
I then connected the buttons action to the outlet of my second view controller.
What I get when I run is this.
It all shows up well but when I try to touch the button that resides in my subview app crashes. I am only left with a worringing: "Action unavailable: The "Touch Up Inside" event of "Rounded Rect Button".
It's probably my logic that is incorrect. Thanks for help.
Well after a long research I got an answer to my problem.
As it appears I was doing everything right.
The problem is that after the initial XIB file is initialized it autoreleases all views and subviews. So to prevent from gettig your second view controller from being released implement this method
- (void)awakeFromNib {
[self retain];
}
in your view controller .m file.
This method will retain your second view controller alive and allow it to recive and respond to UI actions.

Present a UIView over a view

How can I make a custom view in iOS which appears above the existing view,but smaller? It should be like UIAlertView, but taken from a .xib file. After user taps a certain button, the small view vanishes and the normal view appears.
If this is posiible, how can I do it? And.. if it's not hard for you, please, include code.
Thanks in advance!
I think what you're looking for is a modal view. Modal views make it easy to have a view take over the screen for a little while, then when they get dismissed have the background view resume where it left off without having to worry about who's on top or handling events in partially-obscured views.
Here is Apple's article describing it.
They key is to have the controller class for your view call [self presentModalViewController:popupController animated:YES]; where "popupController" is the view controller for the view you want to show up. When you're ready to make it go away, call [self dismissModalViewControllerAnimated: YES];
You can just use the addSubview: method on your UIWindow or your visible UIViewController's view to show the UIView, and you can hide/remove it later by calling removeFromSuperview on the presented UIView.

Catch a subview touch event

I have a UIViewController, and I've added two subviews to its view. One subview is the view of a UIViewController. The other subview is a UITextField.
I need to dismiss the keyboard for the UITextField when the user touches the other view, but I can't figure out how to detect those events. The UIViewController's tableView catches them and breaks the UIResponder chain, so my UIViewController never hears about them. I don't want to subclass everything in the hierarchy just so I can pass the event along up the chain, so what are my options?
I should mention that I'm doing everything programmatically, no IB.
Thanks guys.
So after digging into it, I don't think there's any other way. The responder chain starts with the UIView that received the touch event, if it's not caught passes to that view's controller, then to its superview and so on.
Obviously subclassing every UIView element in a UITableView is insane overkill for this situation.
What I did was create a transparent "touch shield" view with the same frame dimensions as my table view. When the keyboard expands, I add this view over my table, and when it collapses I remove it. This allows me to intercept those touches before they hit the table.

iPhone - Have the keyboard slide into view from the right like when editing a note in Contacts

I'm looking for a way to slide the keyboard into view from the right, like what happens in the Contacts application when you edit a note.
My problem is that when I call [someTextView becomeFirstResponder] in viewWillAppear, the keyboard immediatly pops up with no animation. And when I call it in viewDidAppear, the view first slides in from the right (UINavigationController does the sliding), and then the keyboard slides in from the bottom.
Is it possible to have the keyboard slide in from the right, together with the view?
Solution
In iOS 7, calling becomeFirstResponder on _textView in viewDidLayoutSubviews works.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[_textView becomeFirstResponder];
}
Note: Doing it in viewWillLayoutSubviews also works.
Explanation
Read the discussion in the docs for becomeFirstResponder.
You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.
When using a navigation controller to push your custom view controller onscreen, self.view.window is still nil by the time either viewDidLoad or viewWillAppear: is called. So, _textView.window is also nil in the same methods, since _textView is a subview of self.view, i.e., they're both in the same window. No matter how you present your custom view controller, self.view.window (and thus _textView.window) is also nil in initWithNibName:bundle:. self.view.window is set by the time viewDidAppear: is called, but that's too late because by that time, the navigation controller has already completed the animation of pushing the view onscreen.
self.view.window is also set by the time either viewWillLayoutSubviews or viewDidLayoutSubviews is called and these methods are called before the push animation of the navigation controller begins. So, that's why it works when you do it in either of those methods.
Unfortunately, viewWillLayoutSubviews and viewDidLayoutSubviews get called a lot more than just on the initial navigation controller push. But, navigationController:willShowViewController: and willMoveToParentViewController: get called too soon (after viewDidLoad but before self.view.window is set) and navigationController:didShowViewController: and didMoveToParentViewController: get called too late (after the push animation).
The only other way I can think of doing it is to somehow observe the window property of _textView so that you get notified when it changes, but I'm not sure how to do that since window is readonly.
All you need to do is tell the text view in question to become the first responder in the viewDidLoad method of the view controller you're pushing onto the navigation stack:
override func viewDidLoad() {
super.viewDidLoad()
someTextView.becomeFirstResponder()
}
This works in iOS 8. The keyboard slides in from the right along with the view.
In iOS 7 (or any version before) you can make a simple thing in loadView, viewDidLoad or viewWillAppear
[yourTextView performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
In this case you will get left-to-right appearance of the keyboard aligned with the motion of pushing view controller.
For iOS 7 I've found the following solution to work the best for me:
-Import UIResponder-KeyboardCache to your project.
-Add [UIResponder cacheKeyboard:YES]; to the viewDidLoad of the view before the keyboard view. It might be better to do this immediately when the application loads or during a time convenient when you can afford it (during an HTTP request, for example). In most cases, simply in the view before is sufficient.
-Add the following to the viewDidLoad of the keyboard view.
dispatch_async(dispatch_get_main_queue(), ^{
[_textField becomeFirstResponder];
});
To explain, this will preload the keyboard view, which will remove the delay from the first call of the keyboard view. Calling becomeFirstResponder on the text field in the main queue causes it to slide in with the view instead of animating upward before the view slides in.
You could try sending the becomeFirstResponder message to the new view controller before you push it onto the stack. For example:
-(void)functionWhereYouPushTheNewViewController {
yourNewViewController *newVC = [[yourNewViewController alloc] init];
[newVC.yourTextView becomeFirstResponder];
[self.navigationController pushViewController:newVC animated:YES];
}
I have found that changing animations on things like they keyboard is pretty tough though, and if you read the Human Interface Guidelines Apple makes it pretty clear that they want certain things to act in certain ways, all the time. There are ways to change the behaviors of certain animations but they often involve undocumented API calls and are grounds for rejection from the app store. It would be a violation of HIG to have pushed views slide up from the bottom, for example.
Hope this helps.