Drag & sweep with Cocoa on iPhone - iphone

I'm about to start a new iPhone app that requires a certain functionality but I'm not sure if it's doable. I'm willing to research but first I just wanted to know if I should at least consider it or not.
I haven't seen this in an app before (which is my main concern, even though I haven't seen too many apps since I don't own an iPhone), but an example would be the iPhone shortcuts panels: you can hold on an app, and then drag it to another panel, sweeping while still dragging it. But this is the core app, is it possible to reproduce something similar within a normal app?
I only need to be sure it can be done before I start digging, I don't need code examples or anything, but if you have some exact resources that you consider helpful, that would be appreciated.
Thanks!

Yes. If you have your custom UIView subclass instance inside a UIScrollView, your view controller just needs to set the UIScrollView to delay content touches and not allow it to cancel touch events.
[scrollView setCanCancelContentTouches:NO];
[scrollView setDelaysContentTouches:YES];
When the user taps and holds in the custom view, the event goes to that custom view, which can process the touch events to drag an item around, but if the user quickly swipes, it scrolls the view.

The "panel" view that you're referring to appears to be a UIPageControl view — although, perhaps, the specific incarnation of this view that Apple uses for the iPhone's home page may be customized.
Instances of generic UIView views that you might touch-and-drag will receive touch events. By overriding methods in the view, these events can be processed and passed to the page control, in order to tell it to "sweep" between pages.
If I wanted to do what you're asking about, that's how I might approach it. It seems doable to me, in any case.

Start with this: Swip from one view to the next view
Try using a UIButton that tracks the time since the state of the button changed to "highlighted". You may need to do this in order to track the dragging and move the button around:
Observing pinch multi-touch gestures in a UITableView
Check to see if the button starts overlapping one side of the screen while being dragged. If s certain amount of time elapses since the button first started overlapping the edge and then manipulate the UIScrollView so that it switches to the next page on the corresponding side of the screen
You may need to use NSTimer to keep track of how long the button is held down, etc.
In any case there's no reason why this couldn't work.
If UIButton doesn't work then perhaps try a custom subclass of UIControl (which tracks the same touch down actions etc.). If that doesn't work then use the window event intercept thing to track everything.

Related

Is Selecting Multiple Items with a Two-Finger Pan Gesture only possible with UITableViewController?

I tried implementing the selecting multiple items with a two-finger pan gesture. However, the checkmarks didn't always appear and disappear when tapping edit to start the process, or tapping done when finished.
I later discovered that it works fine when using a UITableViewController after choosing the different controller from cocoa touch menu, instead of the UIViewController and UITableView I was using before.
So my question is: is it correct for me to now assume that these gestures when used in a table are really meant for a dedicated table view controller (with all the extra functionality you only get from it)?
Without any example code, I can't really see what might be going wrong. Have a look at the documentation to see if you are implementing it correctly.
https://developer.apple.com/documentation/uikit/uitableviewdelegate/selecting_multiple_items_with_a_two-finger_pan_gesture

How to transmit UIEvent to UIResponders below

I have many small UIButtons and a large one that covers them all.
The small UIButtons should all react to UIControlEventTouchDownRepeat and call a selector. The big UIButton only has to react to UIControlEventTouchDragInside. The big UIButton however, consumes all touches, and the UIControlEventTouchDownRepeat event doesn't reach the small buttons underneath. How can I solve this?
If the big UIButton is covering the small buttons than the short answer is you can't. Events are passed up the responder chain and when it comes to views the events is passed along to its superview unless the event is handled. Since a view only can have one superview what you want to do is not possible, at least not easily.
If you can reverse the layout, having all the small buttons on top of the the big button it could work. I'm not sure if the events would behave as you would like, you just have to test it out. But all the events not handled by a small button should be passed along to the big button.
If you can capture the point that was touched for UIControlEventTouchDownRepeat then you should be able to find which small button is below the touch and pass along the event that way. You would have to ask the superview of the small buttons, which perhaps is the same as the superview for the big button?, for the view at the touched coordinate and the do something to that view.
Possibly you would want to look into an alternative solution, perhaps creating a custom control that handles the touches as you want.

How do I properly clean up a UIButton

I am having issues with UIButton with the iPhone SDK. I've determined that when I have a UIButton held down when the parent is removed (and the uibutton subsequently removed), I get some strange behavior. In my case, the app stops receiving any input whatsoever, as if the cleaned up held-down button has hijacked the input system somehow.
Does anyone know if there is an appropriate way to clean up the UIButton that would release what I'm guessing is the framework thinking the released button is still held down?
Update: Adding detail to my issue
Basically, the simplified situation is that I have a root view with two characters (actors, not letter characters) and a number of buttons, among other views. The user can use the buttons to affect direction of movement, and when the characters are close to each other, the scene moves forward immediately into another mode, i.e. the buttons slide off, the view cleans up, and I construct a new one.
What this necessitates is the ability to immediately move on and clean up even if the button is still held down. I figured I could pull some tricks to delay cleaning the button up, but I would be surprised if there isn't simply some way to properly clean up the button for removal regardless of what state it is currently in. Hope that clarifies.
It might help if you would explain what behavior you are expecting. If you are calling -removeFromSuperview while holding down the view you want to remove (or its parent), I would suggest you instead hide the view and set some flag you can look for when the user finishes holding that will at that point call -removeFromSuperview.
It turned out the problem wasn't a button being held down but rather a call to set the center coordinate of a mapview with animation on, i.e.
[mMapView setCenterCoordinate:mCoordinates animated:YES];
If the view was cleaned up while the animation was still moving, the app would no longer respond to user input. I'm not certain why this is the case, still, but setting the animated flag to NO ensured the problem didn't arise.

UIScrollView with embedded UIWebView not scrolling after holding

I have a UIWebView which is embedded in a UIScrollView. The webView is resized so that the scroll view manages all the scrolling (I need control over the scrolling).
In the webView I have disabled userSelection via '-webkit-user-select: none;'
Everything is working fine except one annoying detail. When I hold down my finger on the content before starting to scroll for about a second the scrollView won't scroll. My best guess is, that it has something to do with userSelection. The time is about the same it usually takes for the copy/paste/magnifying-thing to appear which usually disables scrolling as well.
I am running out of ideas on how to solve this. Every help would be greatly appreciated!
Thanks!
EDIT: Another aspect of the problem is, that the non-scrolling actually triggers JS-Eventhandler (click, mousedown, mouseup) inside my webView which leads to surprising app behavior. The user puts her finger down, waits, scrolls, nothing happens, removes her finger and this is perceived as a click, which feels wrong from a users perspective.
I would guess what is happening is that after that short duration, the scrollview is no longer interpreting the touch as being on it's view and instead passes the touch down to it's content views.
Have you tried delaying the content touches for the scrollview? This will essentially tell the scrollview to delay taking action on the touch event and instead to briefly monitor the touch and if the touch moves then it recognizes it as a swipe gesture for scrolling. If it doesn't move, it will eventually pass the touch along to it's subviews.
scrollView.delaysContentTouches = YES;
I think even then, there is a standard delay time before the scrollview will pass the touch events along the responder chain. If you hold for too long, it's going to naturally perceive it as being a press down event rather than a scroll event.
This question is not relevant anymore. As of iOS 5.0 the UIWebView is based on a real UIScrollView and also exposes that UIScrollView via a property. Use that instead.
And don't mess with UIWebViews embedded in UIScrollViews anymore. The documentation explicitly advises against that.
Relevant Documentation

How to intercept touch events globally?

I have an view which is sometimes covered by some other views. However, if the user slides the finger across the screen, I want to slide that underlying view across the screen, too.
I could start making custom views for all those covering subviews and forward all kinds of touch events, but that's somewhat cumbersome. Maybe there's some kind of notification or another way that a UIView or UIControl subclass can be aware of touch events happening right now, no matter where they are.
In short: I need an UIView subclass or UIControl subclass which knows about any touch events happening on the entire screen. Or at least if tht's not possible, knowing about any touch events happening above itself in the same underlying superview.
Another description: There are 20 views, all reside inside the same superview. The first view is covered by 19 others. But if the user slides across the screen, that first view must slide too, so it must be aware of touch events.
Is there any better solution that making all 19 views forward touch events? (yes, all 19 views respond to touch events in this example)
Perhaps the hitTest:withEvent: method of UIView will help, it's helped me achieve what you are trying in the past.