Does anyone know why it will not rotate the sprite?
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint center = CGPointMake(CGRectGetMidX([[touch view] bounds]), CGRectGetMidY([[touch view] bounds]));
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint currentTouchPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint previousTouchPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint line2Start = currentTouchPoint;
CGPoint line1Start = previousTouchPoint;
CGPoint line2End = CGPointMake(center.x + (center.x - line2Start.x), center.y + (center.y - line2Start.y));
CGPoint line1End = CGPointMake(center.x + (center.x - line1Start.x), center.y + (center.y - line1Start.y));
CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;
CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);
CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
CGFloat angleInRadians = (line2Slope > line1Slope) ? degs : -degs;
[g setRotation:angleInRadians];
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
UIGestureRecognizer *recognizer;
[recognizer setState:UIGestureRecognizerStateEnded];
}
G is the sprite in [g setRotation:angleInRadians];
Then, when I add in this
[[touch view] setTransform:CGAffineTransformRotate([[touch view] transform], [grinder rotation])];
It rotates the entire scene! Any help, please?
Thank You!
Dude
below is the link for the ui-kit but it will work fine for cocos2d as well
You need to take care of the coordination system
1)For cocos2d origin is bottom left corner
2)For UIKit origin is upper left corner
"Rotate image by Dragging"
Hope This help You
Good Luck
in the init method pass the following code. this will create a gesture recognizer
UIRotationGestureRecognizer *rotationGesture =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:#selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];
this is the vent that will trigger
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform,
recognizer.rotation);
[recognizer setRotation:0];
}
}
Related
Hi friends in my app i want to allow users to resize,rotate and flip an image,
here i have used the below code to resize,
CGFloat angleToRotate ;CGPoint currentTouchPoint,previousTouchPoint; UITouch *touch;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
touch = [[event allTouches] anyObject];
//if (![touch.view isKindOfClass:[vw class]]) {
touchStart = [[touches anyObject] locationInView:self];
kResizeThumbSize=15;
isResizingLR = (self.bounds.size.width - touchStart.x < kResizeThumbSize && self.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (self.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && self.bounds.size.height -touchStart.y <kResizeThumbSize);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
currentTouchPoint = [touch locationInView:self.superview];
previousTouchPoint = [touch previousLocationInView:self.superview];
CGPoint touchPoint1 = [[touches anyObject] locationInView:self.superview];
CGPoint previous1=[[touches anyObject]previousLocationInView:self.superview];
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CGPoint previous=[[touches anyObject]previousLocationInView:self];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
float deltaWidth1 = currentTouchPoint.x-previousTouchPoint.x;
float deltaHeight1 = currentTouchPoint.y-previousTouchPoint.y;
CGFloat newW= self.superview.frame.size.width+deltaWidth1;
if (isResizingLR) {
self.superview.frame = CGRectMake(self.superview.frame.origin.x, self.superview.frame.origin.y,self.superview.frame.size.width+deltaWidth1,self.superview.frame.size.height+deltaHeight1);
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,self.frame.size.width+deltaWidth,self.frame.size.height+deltaHeight);
[self setNeedsDisplayInRect:CGRectMake(self.frame.origin.x, self.frame.origin.y,self.frame.size.width+deltaWidth,self.frame.size.height+deltaHeight)];
}
if (isResizingUL) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width - deltaWidth, self.frame.size.height - deltaHeight);
self.superview.frame = CGRectMake(self.superview.frame.origin.x + deltaWidth1, self.superview.frame.origin.y + deltaHeight1, self.superview.frame.size.width - deltaWidth1, self.superview.frame.size.height - deltaHeight1);
[self setNeedsDisplayInRect:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width - deltaWidth, self.frame.size.height - deltaHeight)];
}
if (isResizingUR) {
self.frame = CGRectMake(self.frame.origin.x ,self.frame.origin.y, self.frame.size.width + deltaWidth, self.frame.size.height - deltaHeight);
self.superview.frame = CGRectMake(self.superview.frame.origin.x ,self.superview.frame.origin.y+deltaHeight1, self.superview.frame.size.width + deltaWidth1, self.superview.frame.size.height - deltaHeight1);
[self setNeedsDisplayInRect:CGRectMake(self.frame.origin.x ,self.frame.origin.y, self.frame.size.width + deltaWidth, self.frame.size.height - deltaHeight)];
}
if (isResizingLL) {
self.frame = CGRectMake(self.frame.origin.x ,self.frame.origin.y , self.frame.size.width - deltaWidth, self.frame.size.height + deltaHeight);
self.superview.frame = CGRectMake(self.superview.frame.origin.x + deltaWidth1 ,self.superview.frame.origin.y , self.superview.frame.size.width - deltaWidth1, self.superview.frame.size.height + deltaHeight1);
[self setNeedsDisplayInRect:CGRectMake(self.frame.origin.x ,self.frame.origin.y , self.frame.size.width - deltaWidth, self.frame.size.height + deltaHeight)];
CGSize supersize=self.superview.frame.size;
NSLog(#"superview frame in resize: %#",NSStringFromCGSize(supersize));
}
if (!isResizingUL && !isResizingLR && !isResizingUR && !isResizingLL) {
CGPoint center = CGPointMake(CGRectGetMidX([self.superview bounds]), CGRectGetMidY([self.superview bounds]));
// [self.superview setContentMode:UIViewContentModeRedraw];
CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
CGFloat angleInRadians1 = atan2f(touchPoint.y - center.y, touchPoint.x - center.x) - atan2f(previous.y - center.y, previous.x - center.x);
[self.superview setTransform:CGAffineTransformRotate([self.superview transform], angleInRadians)];
}
}
Here i can able to resize and rotate but, when i resize AFTER ROTATION the resize is doing somehing plz help me.
use this code for change view size
[UIView animateWithDuration:2
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
// set your resize image;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
yourview.transform = CGAffineTransformMakeRotation(M_PI_2*1.90);//change 1.90 your transform
[UIView commitAnimations];
I have added one label in imageview and added Pangesture for dragging label.With this code label is dragged within the whole.but i want to restrict the label drag within the frame of uiimageview only.
How can i achieve this? The code for adding lable and gesture is as below.
lblQuote=[[UILabel alloc]init];
lblQuote.frame=CGRectMake(130,380, 400,100);
lblQuote.userInteractionEnabled=TRUE;
lblQuote.backgroundColor=[UIColor clearColor];
lblQuote.userInteractionEnabled=TRUE;
lblQuote.lineBreakMode=UILineBreakModeWordWrap;
lblQuote.font=[UIFont systemFontOfSize:40.0];
lblQuote.tag=500;
lblQuote.text=#"";
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [strQuote sizeWithFont:lblQuote.font
constrainedToSize:maximumLabelSize
lineBreakMode:lblQuote.lineBreakMode];
//adjust the label the the new height.
int nooflines=expectedLabelSize.height/16.0;
lblQuote.numberOfLines=nooflines;
// CGRect newFrame = lblQuote.frame;
//newFrame.size.height = expectedLabelSize.height;
// yourLabel.frame = newFrame;
lblQuote.textAlignment=UITextAlignmentCenter;
UIPanGestureRecognizer *gesture = [[[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)] autorelease];
[lblQuote addGestureRecognizer:gesture];
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
// move label
label.center = CGPointMake(label.center.x + translation.x,
label.center.y + translation.y);
// reset translation
[gesture setTranslation:CGPointZero inView:label];
}
I tried moving label on touches event.code is as below:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pointMoved = [touch locationInView:imgView2];
lblQuote.frame=CGRectMake(pointMoved.x, pointMoved.y, lblQuote.frame.size.width, lblQuote.frame.size.height);
}
But movement is not as smooth as pan gesture recognizer.sometimes touch is in another direction and label movement in some other direction and sometimes more or less movement than touch.
Well this is totally depends on your logics although i wrote a code for you, hope this will help you!!
Make sure your UILabel UserIntraction is set to be YES; and use touch began and touch moved methods instead of pangesture.... see this now
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == lblName)
{
NSLog(#"touch on label");
CGPoint pt = [[touches anyObject] locationInView:lblName];
startLocation = pt;
// startLocation is a CGPoint declare globaly in .h..and lblName is your UILabel
}
}
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == lblName)
{
CGPoint pt = [[touches anyObject] previousLocationInView:lblName];
CGFloat dx = pt.x - startLocation.x;
CGFloat dy = pt.y - startLocation.y;
CGPoint newCenter = CGPointMake(lblName.center.x + dx, lblName.center.y + dy);
//now put if condition for dragging in specific area
CGFloat min_X = lblName.center.x + dx - lblName.frame.size.width/2.0;
CGFloat max_X = lblName.center.x + dx + lblName.frame.size.width/2.0;
CGFloat min_Y = lblName.center.y + dy - lblName.frame.size.height/2.0;
CGFloat max_Y = lblName.center.y + dy + lblName.frame.size.height/2.0;
if((min_X >= imageView.frame.origin.x && max_X <= imageView.frame.origin.x + imageView.frame.size.width) && (min_Y >= imageView.frame.origin.y && max_Y <= imageView.frame.origin.y + imageView.frame.size.height))
{
lblName.center = newCenter;
}
}
}
I have a problem, I want to add some gesture to a uibutton for move and rotate it, I use this code
[self.button addTarget:self action:#selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
[self.button addGestureRecognizer:rotationGesture];
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
// get the touch
UITouch *touch = [[event touchesForView:self.button] anyObject];
// get delta
CGPoint previousLocation = [touch previousLocationInView:self.button];
CGPoint location = [touch locationInView:self.button];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
// move button
self.button.center = CGPointMake(self.button.center.x + delta_x,self.button.center.y + delta_y);
}
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
[recognizer setRotation:0];
}
}
all work I can rotate the button and move the button, the problem is if I rotate the button and then move it...in this case not work, I can move the button but not where I want...where is the problem?
You are calculating the difference between the position of touches relative to the button. This does not work as expected when the button is rotated. Instead you should try computing it relative to the superview of the button or the window.
Instead of this:
CGPoint previousLocation = [touch previousLocationInView:self.button];
CGPoint location = [touch locationInView:self.button];
You should use this:
CGPoint previousLocation = [touch previousLocationInView:self.button.superview];
CGPoint location = [touch locationInView:self.button.superview];
I am trying to drag an image view. I have got little bit of success in doing so, but it is not behaving as i want. I wish that it should move only if touch inside the image and drag it.
But it is moving even if I am touching and dragging from anywhere on the screen.
I have written code like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//retrieve touch point
CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
startLocation = pt;
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];
CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[[self.view.subviews objectAtIndex:0] setFrame: frame];
}
The return value of locationInView method is point relative to the view frame. check it is or not in the view frame first.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect targetFrame = [self.view.subviews objectAtIndex:0].frame;
//retrieve touch point
CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
//check if the point in the view frame
if (pt.x < 0 || pt.x > targetFrame.size.width || pt.y < 0 || pt.y > targetFrame.size.height)
{
isInTargetFrame = NO;
}
else
{
isInTargetFrame = YES;
startLocation = pt;
}
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
if(!isInTargetFrame)
{
return;
}
//move your view here...
}
Try something like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//retrieve touch point
startLocation = [[ touches anyObject] locationInView:self.view];
// Now here check to make sure that start location is within the frame of
// your subview [self.view.subviews objectAtIndex:0]
// if it is you need to have a property like dragging = YES
// Then in touches ended you set dragging = NO
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];
CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[[self.view.subviews objectAtIndex:0] setFrame: frame];
Does anyone have a better way to rotate a sprite with one finger? My problem is that I can not get the sprite to stop rotating after it has been fully rotated twice and I flip my screen 180 degree periodcally (self.rotation = 180;) then flip it back (self.rotation = 0). But, when I flip it to 180 degrees the sprite will not rotate properly.
Any one have any better ideas than this?
CGFloat gRotation;
- (void)update:(ccTime)delta
{
g.rotation = gRotation;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(g.boundingBox, location))
{
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, g.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, g.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
gRotation += currentTouch - previousTouch;
}
}
Thanks
EDIT:
I went in to GameConfig.h and changed #define GAME_AUTOROTATION kGameAutorotationUIViewController to #define GAME_AUTOROTATION kGameAutorotationNone
Then, went in to AppDelegate.m and changed #if GAME_AUTOROTATION == kGameAutorotationUIViewController to #if GAME_AUTOROTATION == kGameAutorotationNone
That fixed the sprite's rotation when I flipped the screen, but I am still having problems stopping the sprite's rotation after two full rotations.
add a new line at the end:
gRotation += currentTouch - previousTouch;
gRotation = fmod(gRotation,360.0); // <<< fix the angle
this maybe fix your problem, because the angle will stay in the 360 range
Other way to rotate with one finger is the UIPanGestureRecognizer (but you still need to keep the angle in the 360 range):
UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:#selector(handlePanFrom:)] autorelease];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer];
...
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
...
}
Take a look at this tutorial for more details (its on drag, but show how to do the pan gesture):
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d