I have written a program that will rotate my imageView as i want (CW or ~CW).
And it is working fine.
but it is responding very slowly, I want it to respond quickly...
can you please help me out of it?
Thanks in advance...!
Following is my code------------------------
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [[event allTouches]anyObject];
CGPoint prevLoc = [touch previousLocationInView:self];
CGPoint loc = [touch locationInView:self];
if (isRotating) {
float prevAngle = atan2(prevLoc.y-self.center.y,
prevLoc.x-self.center.x);
float newAngle = atan2( loc.y-self.center.y,
loc.x-self.center.x);
float finalAngle =angle+(newAngle-prevAngle);
//Apply new transform
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(Angle);
self.transform=rotateTransform;
angle = finalAngle;
}
}
Related
I want to throw an object on touches ended method with force and left to right direction.I created world and body object and calculated swipe distance, angle and force but it is not working properly. Here is my code- --
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"tocuehs began");
UITouch *touch=[touches anyObject];
point1 = [touch locationInView:[touch view]];
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch=[touches anyObject];
point2 = [touch locationInView:[touch view]];
float distance = ccpDistance(point1, point2);
int maxDistance = 50;
CGFloat strength = distance / maxDistance;
CGFloat angle = atan2f(point2.y - point1.y, point2.x - point1.x);
angle = - 1 * CC_DEGREES_TO_RADIANS(angle);
// int force = strength * maxForce;
_body->ApplyLinearImpulse(b2Vec2(10.0f+cos(angle)*25.0f,10.0f+sin(angle)*25.0f), _body->GetPosition());
}
Please help!
I have written a custom UIGestureRecognizer which handles rotations with one finger. It is designed to work exactly like Apples UIRotationGestureRecognizer and return the same values as it does.
Now, I would like to implement the velocity but I cannot figure out how Apple defines and calculates the velocity for the gesture recognizer.
Does anybody have an idea how Apple implements this in the UIRotationGestureRecognizer?
You would have to keep reference of last touch position and it's timestamp.
double last_timestamp;
CGPoint last_position;
Then you could do something like:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
last_timestamp = CFAbsoluteTimeGetCurrent();
UITouch *aTouch = [touches anyObject];
last_position = [aTouch locationInView: self];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
double current_time = CFAbsoluteTimeGetCurrent();
double elapsed_time = current_time - last_timestamp;
last_timestamp = current_time;
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.superview];
CGFloat dx = location.x - last_position.x;
CGFloat dy = location.y - last_position.y;
CGFloat path_travelled = sqrt(dx*dx+dy*dy);
CGFloat sime_kind_of_velocity = path_travelled/elapsed_time;
NSLog (#"v=%.2f", sime_kind_of_velocity);
last_position = location;
}
This should give you some kind of speed reference.
i wanted to apply impulse when i release my finger but however i couldn't get it to work. the object body will not re-act to my input. so here's my code for release.
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint != nil) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
float distanceX = location.x-170;
float distanceY =location.y-270;
float distance = sqrtf(distanceX*distanceX+distanceY*distanceY);
CGFloat angle = atan2f(distanceY,distanceX);
// Apply an impulse to the body, using the angle
tailBody->ApplyLinearImpulse(b2Vec2(-distance*cosf(angle)/4+100,-distance*sinf(angle)/4+100), tailBody->GetPosition());
if (mouseJoint) {
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}
is there something missing from the code?
I'm making a simple dial that rotates as you drag your finger across it. It rotates great, but it also rotates when i touch anywhere on the screen and drag my finger.
How can i restrict the first touches to be only inside my imageview object? or where am i going wrong?
this is my code of trouble:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImage *image1 = [UIImage imageNamed:#"nav#2x.png"];
wheelfrom = [[UIImageView alloc] initWithImage:image1];
wheelfrom.frame =CGRectMake(10, -130, 300, 300);
[self addSubview:wheelfrom];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
firstLoc = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
CGPoint curLoc = [touch locationInView:self];
float fromAngle = atan2( firstLoc.y-wheelfrom.center.y,
firstLoc.x-wheelfrom.center.x );
float toAngle = atan2( curLoc.y-wheelfrom.center.y,
curLoc.x-wheelfrom.center.x );
float newAngle = angle + (toAngle - fromAngle);
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
wheelfrom.transform = cgaRotate;
angle = newAngle;
}
Thanks for your help!
You try like this,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(wheelfrom.frame, location))
{
//do your things
}
}
You can try by checking if the point of touch is within the frame of the image view.Do what you want only if its yes.
Inside -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event, check the firstLoc is within your range.
I am wondering what is the best way to implement rotation-based dragging movements in my iPhone application.
I have a UIView that I wish to rotate around its centre, when the users finger is touch the view and they move it. Think of it like a dial that needs to be adjusted with the finger.
The basic question comes down to:
1) Should I remember the initial angle and transform when touchesBegan is called, and then every time touchesMoved is called apply a new transform to the view based on the current position of the finger, e.g., something like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self]; //current position of touch
if (([touch view] == self)
&& [Utility getDistance:currentPoint toPoint:self.middle] <= ROTATE_RADIUS //middle is centre of view
&& [Utility getDistance:currentPoint toPoint:self.middle] >= MOVE_RADIUS) { //will be rotation gesture
//remember state of view at beginning of touch
CGPoint top = CGPointMake(self.middle.x, 0);
self.initialTouch = currentPoint;
self.initialAngle = angleBetweenLines(self.middle, top, self.middle, currentPoint);
self.initialTransform = self.transform;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self]; //current position of touch
if (([touch view] == self)
&& [Utility getDistance:currentPoint toPoint:self.middle] <= ROTATE_RADIUS
&& [Utility getDistance:currentPoint toPoint:self.middle] >= MOVE_RADIUS) { //a rotation gesture
//rotate tile
float newAngle = angleBetweenLines(self.middle, CGPointMake(self.middle.x, 0), self.middle, currentPoint); //touch angle
float angleDif = newAngle - self.initialAngle; //work out dif between angle at beginning of touch and now.
CGAffineTransform newTransform = CGAffineTransformRotate(self.initialTransform, angleDif); //create new transform
self.transform = newTransform; //apply transform.
}
OR
2) Should I simply remember the last known position/angle, and rotate the view based on the difference in angle between that and now, e.g.,:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self]; //current position of touch
if (([touch view] == self)
&& [Utility getDistance:currentPoint toPoint:self.middle] <= ROTATE_RADIUS
&& [Utility getDistance:currentPoint toPoint:self.middle] >= MOVE_RADIUS) { //will be rotation gesture
//remember state of view at beginning of touch
CGPoint top = CGPointMake(self.middle.x, 0);
self.lastTouch = currentPoint;
self.lastAngle = angleBetweenLines(self.middle, top, self.middle, currentPoint);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self]; //current position of touch
if (([touch view] == self)
&& [Utility getDistance:currentPoint toPoint:middle] <= ROTATE_RADIUS
&& [Utility getDistance:currentPoint toPoint:middle] >= MOVE_RADIUS) { //a rotation gesture
//rotate tile
float newAngle = angleBetweenLines(self.middle, CGPointMake(self.middle.x, 0), self.middle, currentPoint); //touch angle
float angleDif = newAngle - self.lastAngle; //work out dif between angle at beginning of touch and now.
CGAffineTransform newTransform = CGAffineTransformRotate(self.transform, angleDif); //create new transform
self.transform = newTransform; //apply transform.
self.lastTouch = currentPoint;
self.lastAngle = newAngle;
}
The second option makes more sense to me, but it is not giving very pleasing results (jaggy updates and non-smooth rotations). Which way is best (if any), in terms of performance?
Cheers!
It is actually much simpler than what you have tried.
You need three data points:
The origin of your view.
The location of the current touch
The location of the previous touch
The Touch object passed to you actually contains the last touch location. So you don't need to keep track of it.
All you have to do is calculate the angle between two lines:
Origin to Current Touch
Origin to Previous Touch
Then convert that to radians and use that in your CGAffineTransformRotate(). Do that all in your touchesMoved handler.
Here is a function to calculate what you need just that:
static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
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))));
return (line2Slope > line1Slope) ? degs : -degs;
}
Courtesy of Jeff LaMarche at:
http://iphonedevelopment.blogspot.com/2009/12/better-two-finger-rotate-gesture.html
Example:
UITouch *touch = [touches anyObject];
CGPoint origin = [view center];
CGFloat angle = angleBetweenLinesInRadians(origin, [touch previousLocationInView:self.superview.superview], origin, [touch locationInView:self.superview.superview]);
Have you considered using UIRotationGestureRecognizer? Seems like that has the logic already baked in, and might make things simpler.