I am trying to figure out the math for rotating a UIView a certain side is always “follows” a finger when moving on the screen.
I’m almost there but I haven’t had trigonometry in years and I can’t figure out exactly what to do. Here is what I’ve got:
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
int dy = cannon.frame.origin.y - touchPoint.y;
int dx = cannon.frame.origin.x - touchPoint.x;
cannon.transform = CGAffineTransformMakeRotation(atan2(dy,dx) - 135);
}
First, thank you to everyone who took the time to answer!
I played around with it for a while, and it seems that the only problem was that I used
ints fordyanddxinstead offloats. I also found a nifty, quick “method” for converting degrees to radians. Here is the final, working code: