iphone touchesbegan - iphone

I am using touchesBegan:.
I touch an object and drag-and-drop it to some other place, but I also want to make a copy of it on its original. That is, after touching and dragging, there should be a copy of it at its original position.
Below is my code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[[event allTouches]anyObject];
CGPoint location=[touch locationInView:touch.view];
image1.center=location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}

Instead of moving image1 create and image2 (in touchesBegan) and move it (in touchesMoved)
You have to get touches from the superview of the object you want to move I believe.
You didn't show what image1 is, hence nobody can tell certainly how to copy that.

Related

Get the touch event to a specific area like photo gallery in iphone

In my app for iPhone/iPad (in Objective-C), there are some UiButton's and other events and an UiImageView. Image covers the whole screen and i want that when one touches the upper or bottom area of screen(means suppose one button in the upper area), then the image resizes to a specific size.
But i am not able to recognize if one touches the upper area or not, like in the Photo Gallery of the iPhone(where if i touches the upper/lower area the navigation button appears, the same thing I want in my app).
I already have the touch events in my app:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
If somebody has any idea or solution please let me know.
The most obvious solution would be to subclass the UIImageView, then override it's touchesBegun:withEvent: method and in that method check where the actual touch happened:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = (UITouch *)[touches anyObject];
CGPoint location = [touch locationInView:self];
if (location.y > some_arbitrary_value || location.y < some_other_arbitrary_value)
{
//touched upper or lower part
}
}

actions on touchesMoved in cocos2d-iphone

I'm trying to start developing cocos2d games. So, I'm new in cocos2d, but I developed few applications on iPhone. I installed cocos templates (v2.0) and created new project with box2d phisics. Here I can see a demo project with blocks and some menus. When I tap screen, new block appears, and falls to the botton of screen. Than must be implemented here:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
[self addNewSpriteAtPosition: location];
}
}
so, sprite appears when touches ended. But how to do something when touches begun or moved? I cant this finds methods for cocos. I saw some tutorials, there is method like this:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
but it never called... What am I doung wrong?
Implement
(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
And ensure you have
self.isTouchEnabled = YES;.
in your init method for that layer.
Implement:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// whatever is needed here
return YES;
}
in your cocos2d class (the same implementing ccTouchesEnded), then it will be called.

iphone UIImage resize by endpoints

I want resize the shapes draw on context by picking their endpoints. And also rotate it with its endPoints. Can anybody suggest me how can I do this with proper example?
My edited Question.
According to the image I have to rotate and scale the shapes by its Endpoint
when the user touches the Endpoint, you can get the coordinates of the touch in touchesbegan: and the frame and transformation of the image. When you move, you get a new coordinate for the touch in touchesmoved:.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchStart = [[touches anyObject] locationInView:viewThatContainsImage];
self.imageStartFrame = image.frame;
self.imageTransformStart = image.transform;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint newTouchPoint = [[touches anyObject] locationInView:viewThatContainsImage];
[super touchesMoved:touches withEvent:event];
}
With the starting frame, starting transformation and the starting point and current point, you can figure out the new size/angle change.

iphone - UIView with labels, detect touch when moved fingers on it

I've a view with 10 character labels (big letters). Each character (label) has a different tag. The functionality that I'm developing is "Trace".
When user moves his finger over the view, i want to detect which character is touched.
I think I've to implement,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
But I'm not knowing how to identify touch on the label and identify the character.
Can some one help me?
If you want to know the view that the touch began on (which view the user put their finger down on), you can read any of the touch items returned in the NSSet like so:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSInteger viewTag = [[[touches anyObject] view] tag];
//Check your view using the tags you've set...
}
However, even as the finger moves across the screen, this view property will ONLY return the view initially touched and not the view currently under the finger. To do this, you will need to track the coordinates of the current touch and determine which view it falls into, perhaps like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
//If all your views are collected as an array
for(UIView *view in views) {
if (CGRectContainsPoint([view frame], point))
{
//Bingo
}
}
}
The key to success on this is to make sure that you are getting your touches in coordinates reference to the proper views, so make sure to call locationInView: and CGRectContainsPoint() appropriately with values that match your application's view hierarchy and where your placing this code (i.e. the View, ViewController, etc.)
To detect simple touches you can use UIGestureRecognizer. Read the documentation for more on those. For more complex operations you do need to implement:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
To identify which item was touched you can give your item's tags values:
myView.tag = 4;
Then just check the tag value of the view reporting the touch and you know which it is.

How to create hotlinks on image?

I'm not sure what the iPhone version of this is called but in HTML it is an image map. Certain areas of the map route you to different pages. I'd like to use an image, mostly in a scrollview, that I can have certain areas perform different actions. For example, Chemical Touch, http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=288060442&mt=8, does this. Click on an element and it grows in size to reveal more details. What is this type of technique called and how is it created?
I'd like to be a little more interactive with the user, how can I do any of these:
drop map pins onto this image
display a UITextField depending if I have text in that area
overlay text depending on user actions
Any suggestions here are greatly appreciated.
What you basically need to do is override any or all of:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
in your UIView subclass (or actually any subclass of UIResponder). Then you can handle the event however you wish, such as doing any of the things you mentioned in your question.
For example, here is Apple's sample code for a simple event handler:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
if (numTaps < 2) {
[self.nextResponder touchesBegan:touches withEvent:event];
} else {
[self handleDoubleTap:touch];
}
}
The Event Handling section in the iPhone Application Programming Guide should give you all the info you need to get started.