Desmond
Desmond

Reputation: 5001

how to move an UIImageview from point A to B in a certain degree

I would like to move a UIImageview from point A to B in certain degree. the Red and Blue arrow denote the degree i want the dots to move.

i can move the dots from one point to another, but how do i move the note on a degree ?

    NSLog(@"cgpoint %@",NSStringFromCGPoint(self.aggressiveDots.frame.origin)); 
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], nil];
[anim setKeyTimes:times];   
NSArray *values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(118, 188.)],
                   [NSValue valueWithCGPoint:CGPointMake(115, 300.)], nil];
[anim setValues:values];
[anim setDuration:6.0]; //seconds    
[self.redDots.layer addAnimation:anim forKey:@"position"];

enter image description here

enter image description here

Upvotes: 1

Views: 213

Answers (1)

Jasper Blues
Jasper Blues

Reputation: 28756

Since you'd like to move the UIView along a straight line (not a curve) you only need two points for your CAKeyFrameAnimation - the starting and endoing point. In fact you could even use [UIView transistionWithView:] for this if you wanted - just manipulate the frame in the animation block.

  • You already know the starting point.
  • To calculate the ending point you need to find the point where the edge of the red circle intersects the white circle. There's an equation to solve the intersection points of two circles detailed here

If you'd like to avoid tricky algebra, you can draw a triangle as follows:

  • Just let the first circle travel down the line, using the simple line equation y=mx + b
  • Test if the edge of the circle (which is radius points along the line ahead of the center) is inside the triangle using the formula outlined here

"In general, x and y must satisfy (x - center_x)^2 + (y - center_y)^2 < radius^2"

Upvotes: 1

Related Questions