iphone UIImage resize by endpoints - iphone

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.

Related

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.

Jittery movement when using CGAffineTransformMakeRotation

I'm having a problem with a jumping movement with using CGAffineTransformMakeRotation. It seems to work fine until I get to the top of quadrants 1 and 2. Below is the code I am using and youtube link that shows what happens. Any insight would be really appreciated.
My goal is to rotate a UIWebView with an svg inside. Since I can't easily detect touch on the UIWebView alone, I'm putting a blank UIImageView over it. This allows me to detect the touch and prevent the copy dialog from popping up.
http://www.youtube.com/watch?v=x_OmS0MPdEE&feature=youtu.be
- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSArray *allTouches = [touches allObjects];
CGPoint location = [[allTouches objectAtIndex:0] locationInView:self.view];
if(selected == 1) {
CGFloat rads = atan2f(location.y - (grid1.frame.size.height/2),location.x - (grid1.frame.size.width/2));
grid1.transform = grid1Btn.transform = CGAffineTransformMakeRotation(rads);
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSArray *allTouches = [touches allObjects];
CGPoint location = [[allTouches objectAtIndex:0] locationInView:self.view];
if(CGRectContainsPoint(grid1Btn.frame, location))
{
selected = 1;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
selected = -1;
}
You use some frame.size.* in your computations. Note, that those values (unlike bounds.size.*) are affected by transform. Use grid1.center instead. It is also more logical (you rotate around center).
The next thing you need to fix is that it should not jump to new position when touches start :)
The jerkiness is caused by the inherent inaccuracy of touch event locations.
Touch events that are located sufficiently close to the center of the frame have a high likelihood of straying to the diagonally opposite quadrant of the frame whilst a circle is being traced about the center of the frame.
This will result in a sudden jump of 90 degrees as observed in your video.
One way to avoid the problem is to introduce a dead zone about the center of the frame. Any touch that is located within a given radius of the center of the frame would not trigger the rotation to be recalculated.
Hope this helps.

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.

iphone touchesbegan

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.

move sprite by using touchmoved in specified regions of screen

I am making an application, in that I want to move the sprite in a specified region of the screen, With cocos2d I am not able to make move of sprite, I only know the method -(void)ccTouchMoved:(UITouch *)touches withEvent:(UIEvent *)event, but I don't know how to move a sprite,
can any one help me????
Thanks in advance
Try something like the following.
-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate:
[touch locationInView:touch.view]];
[yourSprite setPosition:ccp(location.x , location.y )];
return kEventHandled;
}
Edit: If you simply want to move the sprite without a touch event simply call
[yourSprite setPosition:ccp(someX, someY)];