Calculating Scale Factor touchesMoved event.
Calculating Scale Factor touchesMoved event.
- Subject: Calculating Scale Factor touchesMoved event.
- From: Gustavo Adolfo Pizano <email@hidden>
- Date: Thu, 2 Dec 2010 16:37:08 +0100
Hello.
Im trying to make a scale up or down of a view with one finger only,
by moving outwards and inwards a control view, which represent a
virtual circled pad..
I have the following code in the control pad
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * aTouch = [touches anyObject];
distAux = dist = [self getCurrentDistanceFromCenter:aTouch];
if((dist >= (radius - 20.0f) && dist <= radius + 30)){
canRotate = YES;
startingAngle = [self getCurrentAngleFromCenter:aTouch
center:CGPointMake(self.frame.size.width/2,
self.frame.size.height/2)];
firstTouchPoint = [aTouch locationInView:self];
}
...
this will save the initial point in the path, and I jsut calculate if
the touch is in a specified area, so I can initiate the scaling.
now in my touches moved I do the following, the first part applies a
rotation, which is working properly, except that after the first move
the angle is 180 from what the view has, so you see that the view
turns 180 degrees!!, dunno why.. anyway, the second part Im trying to
get the distance of the first touch point which i saved in the
touchesbegan, and calculate a distance, then scale by transforming
the bounds of the control pad and using an aux variable to reposition
that initial distance.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
...
CGPoint Location = [[touches anyObject] locationInView:self];
CGPoint p = [self convertPoint:self.center fromView:self.superview];
int x = p.x;
int y = p.y;
float dx = Location.x - x;
float dy = Location.y - y;
//CGPoint p1 = [aTouch locationInView:self];
//CGPoint distance = CGPointDelta(firstTouchPoint,[aTouch
locationInView:self]);
//atan2(p1.y - firstTouchPoint.y, p1.x - firstTouchPoint.x)
CGFloat a = atan2(-dx,dy);
//CGFloat a = atan2(p1.y - firstTouchPoint.y, p1.x - firstTouchPoint.x);
bc_selectedItem.transform = self.transform =
CGAffineTransformRotate(self.transform, a);
actualAngle = a;
rotating = YES;
//Second Part
//Now im gonna chekc if I should enlarge
CGFloat newDistFromCenter = [self getCurrentDistanceFromCenter:aTouch];
CGFloat deltaCenter = newDistFromCenter - dist;
CGFloat deltaCenter2 = newDistFromCenter - distAux;
if([bc_selectedItem canScaleWithScale:deltaCenter]){
// CGRect b1 = bc_selectedItem.bounds;
// b1.size.width += deltaCenter;
// b1.size.height += deltaCenter;
// bc_selectedItem.bounds = b1;
// bc_selectedItem.transform =
CGAffineTransformScale(bc_selectedItem.transform, [bc_selectedItem
getCurrentScaleFrom:deltaCenter],[bc_selectedItem
getCurrentScaleFrom:deltaCenter]);
// modifying the bounds when scaling the control pad gave me better
results because of the scale factor miscalculation
CGRect b = self.bounds;
b.size.width += deltaCenter;
b.size.height += deltaCenter;
self.bounds = b;
dist = newDistFromCenter;
}
[self setNeedsDisplay];
....
So Im have tried applying an Affinetrasnform as shown in the commented
lines, but because im not calculating properly the scale factor
depending on the distance (delta) between the initial point and the
actual point, the results are not as expected.
also in the item that is being scaled using the control pad, this code
calculates the actual scaling, but it seems to be wrong also...
-(CGFloat)getCurrentScale{
CGAffineTransform t = self.transform;
CGFloat a, c, b, d;
a = t.a;
c = t.c;
b = t.b;
d = t.d;
CGFloat scaleX = sqrt(powf(a,2)+powf(c,2));
return scaleX;
}
because Im scaling on Sx and Sy with the same values, Im just
calculating one value.
Any idea how can I calculate the NEW scale factor using a single touch
so the scaling will display smoothly and constant?
Thanks a lot in advance for any help provided.
Gustavo
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden