In XCode, I created a xib file with several image view, some stacked on others. The entire view has a single view controller that has a connection to every image view. I guess my first question is, are all the image views direct subviews of the entire view? Anyways, when ever the over all image view is touched, I detect it with touches began / touches moved. It works perfectly. However, there is one image view in particular that I want to know if it is being touched. How can I, from the master view's touches began / touches moved, find out if a sub view within it was touched? I want to do it this way because everything else I tried did not work. For the sub view, i created a view controller and set its view to the sub view in question. I set up the touches began / touches moved methods inside the controller, but they do not react to touches at all.
For UIImageView instances, you need to explicitly do setUserInterActionEnabled=YES
Related
I want to drag from the masterview tableview Cell to DetailView.
I try with touchesBegan & touchesEnded method but not working.
Can you please help me for this?
Thank you
This is actually not easy. You can start by
Adding a pan gesture recognizer (UIPanGestureRecognizer) to the root view controller's view (UIWindow.keyWindow!.rootViewController!.view).
When the pan starts (i.e. user touches the screen), loop through the master view's table view's visible cells to see if the point is inside any cell by using UIView's convertPoint:fromView:. You may need to adjust timing to avoid interfering with table view's scrolling and tapping.
If a cell contains the pan's point, create an "indicator view" (that shows that user is dragging) and add it to the root view controller's view, on top of everything else and position it properly, e.g. under user's finger.
When the pan changes (i.e. user moves his finger), update the indicator view's location.
When the pan ends (i.e. user releases his finger), check if the point is inside the detail view and do whatever you need to do.
Check this out. It demonstrates how to do drag drop within a view. Your problem is more complicated as it involves different view controllers, hence the touch handling must be done at a level higher than both master and detail view controllers.
Why are you using touchesBegan & touchesEnded methods? If you have the tableView you should use didSelectRowAtIndexPath delegate method. And you can also use segues if you are using storyboards.
I have created a UI which has two child view controllers which slide in as menus from each side of the screen when a button is pressed (think hamburger menu). I have a table view on one and a collection view on the other. Neither will scroll for me or accept touch events. The code below is used to add to the parent container.
sidePanel = sb.instantiateViewControllerWithIdentifier("sidePanel")
self.addChildViewController(sidePanel)
self.view.addSubview(sidePanel.view)
sidePanel.view.center.x += self.view.frame.size.width
sidePanel.view.frame.size.width = 250
sidePanel.view.updateConstraints()
sidePanel.view.layoutIfNeeded()
Any ideas why touch isn't working? I've checked all the obvious solutions (userInteractionEnabled etc.).
The issue is caused by moving the view. The sidePanel is not contained within the view so is not receiving touch events.
Here's my setup:
I have a view containing a small view at the bottom of the screen, which contains a text field. I have added some logic to move the whole view (including the small one) up as soon as the textbox is selected, and down as soon as the textbox disappears.
I have also added a gesturerecognizer to the whole thing, to resign first responder once the user taps somewhere else.
Afterwards, I added a ContentView to all of this, which in turn references a tableviewcontroller. It looks fine: a tableview in the back, the text field in its view at the bottom, once I tap the textfield everything is moved up and down as intended.
However, the gesturerecognizer doesnt work properly. It detects touches to the small view containing the textfield, but not on the tableview.
I have tried adding the gesturerecognizer to the tableview, but it didnt seem to make a difference - the gestures weren't recognized. I also tried adding another view on top of the ContentView - it worked, but it didn't pass the events to the tableview below.
I have created the views with storyboard and added the code to move the views programatically.
The gesture recognizer is working properly, make sure that it doesn't conflict with other event handlers. You can put break point into the method which is handling events to make sure it does really handles those events you wish.
As the alternative, put a blank view in front of table view and attach gesture recognizer. :)
I solved it using the answer provided [here]. I was not able to solve it with the storyboard gesture recognizer without additional code.1
In brief: Is there a way to hand-off an in-progress drag gesture from one view to another?
More detail: I'm working on an iPad app. Imagine a situation where there's a playfield in the middle of the screen. On the borders are a bunch of "pieces" that can be dragged onto the playfield.
When one of the pieces is dragged onto the playfield, I want its appearance to change. Though it might be simplest if I had a single view that transformed its appearance when dragged onto the playfield, in my particular case, it's simpler if there is one view for the off-playfield appearance and another for the on-playfield appearance.
The trick is this: while I'm dragging the piece onto the field, I want the drag to continue on once I've gotten rid of my "off-playfield" view and added my "on-playfield" view. Essentially, I want to hand-off the gesture from one view to another, while the user goes through one continuous drag.
Is there a way to do this?
UPDATE: Actually got this working using the single-view approach, but am still curious as to whether a gesture hand-off can be done...
I think this isn't possible. What is possible is to have a parent view that contains the "before" view and "after" view. As the drag occurs, that parent view can switch between the two views while maintaining the drag.
In my Ipad app, I have a split view in which the detail view is a scroll view...
I have 3 subviews to the scrollview which are tableviews...
How do I use - (void)bringSubviewToFront:(UIView *)view to bring one of the subviews of the scrollview to the front when an action is performed? (since Views are "stacked" in the order they're added with the last one on top). Should I write the code in the subview or in the detailViewController and how do I call it?
You write the code in the controller.
The controller should have access to the table views through IBOutlet properties. Or, if you didn't set them up using a nib, the controller should have been the one to create them.
If a button tap, for example, is responsible for showing a particular table view, the button's action handler method is where you call the bringSubviewToFront: method.
HOWEVER: It sounds like you have three table views on top of each other and are using bringSubviewToFront: to show the current one, and they are all inside a UIScrollView.
Each UITableView contains a UIScrollView. Don't put a scrollview inside a scrollview, they will fight over tracking touches and things will get weird. Just put the three table views inside a plain UIView.
Instead of bringSubviewToFront:, you ought to consider hiding the inactive views (call setHidden:). That way, the hidden views won't be considered part of the responder chain (won't get sent events).