Another drag and drop question on iPhone - iphone

I tried to find an easy solution for the following.
I have a main view which holds a tile. I want to drag and drop it over a UITableView. I could program the pick and drag already with UIGestureRecognizer.
Now my problem is how can I detect within the table view that there is an item going to be dragged above it. For instance I want to highlight the given row when the tile is moved above it.
I tried to add the touchesMoved/Began/Ended events to the viewCell. They does not get fires when I am dragging the tile over it (in other words the tile is hiding a portion of the viewCell under my finger). They get fired when there is no tile dragged above.
Is there an effective hittest method for that?
Thanks.

There are a whole bunch of UI issues here that should be addressed.
In general, Drag and drop on the iPhone is a BAD idea. It's very hard to do, and getting feedback is going to be problematic, because your finger will be obstructing the drag operation
using the table's selected cell to indicate 'dropability' may get you rejected by the app store reviewers; it's to indicate "I've selected this cell", not "I'll drop something onto it"
That said, you'll probably want to manage all your touch events in the 'source' view, and send the table messages from there, rather than trying to juggle messages around. Once your -touchesBegan:withEvent: has been called in one view, all subsequent -touchesMoved:withEvent: and -touchesEnded:withEvent: will be sent to that view for the life-cycle of that touch; forwarding them around will confuse the heck out of everyone involved.

Related

Continuously scrolling UI Unity

So I am working working on something in unity (2d game). I have a list of button (UI) on my scene and I want to implement a scrolling mechanism. What is the best way to go about it? Currently , I can scroll through horizontally because I have added a "scroll rect" to the the canvas holding the buttons as I start the game. However when I try to scroll back (horizontally, it goes beyond the Buttons. Is there a way to make the scrolling continuous such that as I scroll, from the first UI element when I get to the last one and I keep scrolling, it continues with the first element. or what should I do. please let me Know If i should clarify.
In the Scroll rect you can set the movement type. I don't think the kind of "infinite scroll" you are asking for is available.
For your case, I think that the Use Elastic or Clamped mode to force the content to remain within the bounds of the Scroll Rect should do the work.
In the case of the infinite scroll specifically needed, you would need to ask for that explicitely and show your attempt for more specific help.

How to avoid using instanceOf in this case? (allowing clicking on only some objects in a quadtree)

I have a bunch of Tank objects inserted into a quad tree. Some of these tank objects can be clicked on if they implement a Clickable interface. The problem is that in order to know what is being clicked, I need to query the same quadtree, but the quadtree has both clickable and non-clickable objects in it.
Potential solutions:
I could using instanceOf to see which ones in a specified region were clickable when the user clicks the screen, but I hear that using instanceOf is bad practice.
I could maintain TWO quadtrees. one for just tanks, and one for clickable objects. But then I could have to update tanks that implement the clickable interface TWICE since they would be in two seperate quadtrees, which would be slow considering I have to update every step, and people only click the screen every so often.
I could only insert clickable objects into the quadtree, and simply make non-clickable objects define dummy click methods. This would solve the problem, but it doesn't feel right because if something is non-clickable, then it shouldn't be implementing a clicking methods to begin with even if it is an empty one. Or is this ok?
I'm thinking #3 is probably the best way to do it, but I'm not sure. Any pointers?
EDIT: Now that I think about it, #3 would have some problems dealing with overlapped clicks. If a non-clickable tank overlaps a clickable one, and I click in the overlap section, unless I call all click methods for everything at that point, I could end up calling the click method of the wrong tank. To avoid doing THAT, I'd have to use instanceOf and find the clickable object and only call ITS click method.
So maybe #2 is better because I could easily cycle through all clickable objects at the clicked region and choose the one that is either at the top, or closest to the center of the region, and only call that tanks click method.
It could also not necessarily be that they overlap, but are just close together. If I were to make this game touch based, your finger isn't a point, but a region, so it might be better to cycle through objects in a region and find the closest clickable object, which would be hard to do with 3# without sifting through a bunch of non-clickable objects.
#3 sounds pretty good. You could maybe enhance it by creating another interface that ALL your objects implement, for example "GameObject", which has a method "isClickable". Then you can have your clickable objects implement a "return true" for isClickable.

iPhone: Drag to make a new object

Im making an iPad app that will have some objects on the screen in a menu area. I want the user to be able to touch down and drag from one of these objects to place a new copy of that object on the view.
To further explain, say I have a checkers chip on the side of the UIView in the "game piece" / menu area. I want the user to touch down and drag from that checkers piece to drag a new checker piece onto the game board. I dont want the original checker to move, its just a place holder, but I want to create a new object that is dragged from the first checker.
I see a couple of ways to do that however I cant figure out how to do it without the user picking up their finger to reselect the new game piece. I want the new piece to be drag able immediately after touching the first place holder object.
Any ideas? Please and thank you for the assistance.
I've solved this before by creating a copy of the UIView that I wish to move, then using the UIResponder API (touchesBegan, etc.) to move the new copy in its superview based on the drag gesture translation, thereby leaving the original "menu" view in situ. Does this make sense?
When you create the moveable copy, you can set its alpha or create a border or some other visual confection to indicate the provisional nature of the drag object.

Making items draggable

How are objects made draggable on iphone?
How is an item/object moved by the user. e.g. apps can be moved on the homescreen and they icons follow the users finger.
How can e.g. a tableview cell contents be moved if the user holds down on it and then moves their finger?
What controls the movement of these objects to follow a users finger?
There is no built-in support of dragging controls and you need to implement one. My answer to another question is related and might help.
Dragging values onto labels

Drawing rubber band line during user drag

In my iPhone app, I would like the user to be able to "connect" two of my views by:
1) starting a drag in View A
2) as they drag towards View B, a straight line with one end in View A and the other end under at the current drag point, animates in a rubber-band fashion
3) when/if they release in View B, the line is then shown between the two views
I've seen examples of dragging and dropping views, and other examples of animations, but I haven't seen one that is a simple example of this kind of user-directed animation. Any pointers towards examples or the specific docs I should be looking at would be appreciated.
If this turns out to be trivial - my apologies. Although I've done quite a bit of development, I'm just getting started in the iPhone SDK and Core Graphics.
Turns out it is pretty easy - don't think of the line as belonging to either view, create a third view that is transparent and not opaque, place it over the top of the other two views. It could be full screen or you could calculate the size and position that just covers your views. Detect taps in this third view and use core animation to display a line from the point you started drawing to the point the line ends. When the line ends then you can detect whether the input was valid and place start/end points in the appropriate views. Functions that you will find especially useful in this process are UIView convertPoint:toView: and beginAnimations:context:.
It will probably keep things easier if you leave the line drawing as part of a dedicated view and add lines to it as they are accepted, rather than try to record the lines as part of the unrelated views that you are connecting with the lines - probably you want an array or similar containing CGFloats so that you can recreate the line view as necessary using drawRect:.