Is there a class / method to handle dragging views? - iphone

I found a useful tutorial to get started in understanding how to Cocoa handles touch events. I've used this as a base to create a custom script, I'm trying to make a UIView draggable, very similar to the native Maps application.
I've written a custom script, using
the touchesBegan method it will
capture where the input began and
compare it to the centre point of the
UIView using some conditional
statements.
The touchesMoved method will do some
further conditional statements to
determine whether the touch start
point and the center of the view will
move positively or negative.
I've also captured the views
boundaries so it doesn't go too far
out.
it's lacking the polished finished found in other applications such as Maps, or scrolling a UITable, such as the ease effect after the user has released their fingers, and the snapping effect when it reaches the boundaries is horrible.
Is there a method that takes a view and makes it draggable like this? If not I'll continue to refine my script.
Many thanks!

Maybe you are looking for UIScrollView

Related

endless vertical scrolling background

I want to make an endless vertical scrolling layer that gives the impression that the main character is moving upwards. I have been brainstorming on how to achieve this.
My issue is that I want objects to appear as if they are coming from above and below the screen at the same time. Secondly, I want to be able to move the main character to create and destroy box2d joints between it and some of the objects appearing on the screen. What is the best way to achieve this with consuming too much memory? I would appreciate any help on this.
Apple did a wonderful tutorial of this in a WWDC 2011 video session. It was "UITableView Changes, Tips & Tricks" and it's about 35m40sec into the video.
Since the use of the UITableView is really just a UIScrollView for the purposes of the background, you could just use a UIScrollView and you can either have it move on timer or events as needed.
Think of your player as moving within a stationary bounding box. The background can scroll using the aforementioned pooling method (as the background tile scrolls off the screen it is placed into a pool, and before a new tile is instantiated the pool is checked for available reusable tiles). Thirdly, your enemy objects will simply approach from either the bottom of the screen or the top.
Imagine your idea without the scrolling background (flying effect) and you should find that the problem is relatively straightforward.
I also needed and endless scrolling background layer. This can do exactly that, and it is super simple to set up and use. Just copy the four files in to the cocos2d folder in your project, then follow the quick tutorial seen on the github. Make sure the image you use is seamless (when you line them up vertically you can't tell where one ends.

drawing a line from gesture between 2 buttons

I am beginner for interactions on iphone. I have 3 buttons, one on the left, and the 2 other ones on the right. I would like to push the left button with finger and display a line real time stretching with my finger moving on screen that goes to the other button I am going to go to on the right - and then I reach the button on the right and release my finger the line stays on screen.
Where should I start in the manual to understand how to do something like this?
- please don't say page 1 ;-)
thanks for all the pointers so I can learn and write the code myself
Cheers,
geebee
Wow, uh, you're jumping right into the deep end.
First, the answer to your question is ... check out the Sample Code at developer.apple.com. By playing with those sample projects, and running them in the debugger, and reading that code, you can learn a whole lot about what you're trying to do.
Using those samples as your tutorial, refer to specific information in the reference docs also available at developer.apple.com. There are specific docs on the classes you'll be using (e.g. UIButton Class Reference), and there are high level guides that talk about different areas, like the Event Handling Guide for iOS, and Drawing and Printing Guide for iOS.
Here are some of the issues you're going to face, and some of the areas that you should read up on:
UIButtons are going to respond to your touch and won't allow any other view to receive that touch without some special handling in your code.
Check out samples that deal with UITouch events, Quartz 2D drawing, creating custom controls
Try doing this without any buttons first, that's fairly simple. Just figure out how to detect a TouchDownInside event, track the finger movement across the screen, and detect a TouchUpInside event, all in a normal UIView. (There may be a sample for this.)

Cocos2D iPhone - Handling Touches Correctly

I am not a newbie to Cocos2D but I am building quite an advanced HUD with several sliding and overlapping CCLayer and CCMenu/CCMenuItemImage objects.
They are all responding to touches correctly in turn. However when things overlap, it seems the buttons underneath take priority over the things on the top, no matter what order I add them to the world.
Indeed, even implementing the registerWithTouchDispatcher method and returning YES/NO ccTouchBegan:withEvent: seems not to have the correct effect. It also appears that ccTouchBegan:withEvent: is then called on all buttons/menus in the world rather than just those underneath the touch.
I'd really like advice on a reliable way to detect and consume a touch on an object that is top most in the view without anything else hearing about the touch.
Thanks in advance!
How about this commit for develop branch of cocos2d-iphone?
v1.0.0-rc3 or earlier doesn't have the mechanism for touch priority. This commit seems to implement it.
Why can't you use tags? I'm not sure at the moment how to check z order but I would personally probably just use tags.

Advance way of using UIView convertRect method to detect CGRectIntersectsRect multiple times

I recently asked a question regarding collision detection within subviews, with a perfect answer. I've come to the last point in implementing the collision on my application but I've come across a new issue.
Using convertRect was fine getting the CGRect from the subView. I needed it to be a little more complex as it wasn't exactly rectangles that needed to be detected.
on XCode I created an abstract class called TileViewController. Amongst other properties it has a IBOutlet UIView *detectionView;
I now have multiple classes that inherit from TileViewController, and each class there are multiple views nested inside the detectionView which I have created using Interface Builder.
The idea is an object could be a certain shape or size, I've programatically placed these 'tiled' detection points bottom center of each object. A user can select an item and interactive with it, in this circumstance move it around.
Now the method itself works to an extent but I don't think it's working with the nested values properly as the detection is off.
A simplified version of this method works - Using CGRectIntersectsRect on the detectionView itself so I'm wondering if I'm looping through and checking the views correctly?
I wasn't sure whether it was comparing in the same view but I suspect it is, I did modify the code slightly at one point, rather then comparing the values in self.view I took the viewController.detectView's UIViews into the interactiveView.detectView but the outcome was the same.
It's rigged so the subviews change colour, but they change colour when they are not even touching, and when they do touch the wrong UIviews are changing colour
I worked out my issue when using convertRect.
I thought I'd read the documentation again (believe me I've been reading it) but I missed a key piece of information previously. To use convertRect:toView: to use the method the view needs to be target of the conversion operation as mentioned in the doc, but I was using the view itself as the target instead of the parent view
interactRect = [detectInteractView convertRect:[detectInteractView frame] toView:parentView];
This is wrong, I know there isn't many details in this post but ultimately you can't use the same UIView as the target view, or at least if you can I couldn't get it to work here!

How to draw with a finger or the iPhone

does anyone have a working sample project showing how can a user can draw simple sketches on the iPhone, with his finger? Something like the GLPaint, but easier, without Open GL.
Something just with the basic drawing functions. I know there was a FingerSketches sample time ago, but it appears it's no longer available.
You're right, there was a FingerSketch example; I'm not sure whether it still works, and I don't think it's a good idea to publish the entire example here. But I think you can use the Touches or MoveMe samples as a starting point to learn how to react to touches.
Basically, you have to remember the last position on each touch event, and draw a line from the last to the current position in touchesMoved and touchesEnded. As usual, FingerSketch doesn't draw directly to the screen, but to a separate drawing context, and then calls [self setNeedsDisplay] to redraw itself.