, , , , 100% , . , , :
- (CGFloat) pointToAngleFromCenter: (CGPoint) point {
point.x = point.x - self.center.x ;
point.y = self.center.y - point.y ;
return ::atan2(point.y, point.x) ;
}
- , .
What he does is that he takes a point in the coordinates of the parent view, redirects it relative to the center of the view (which is in the coordinate of the parent view) and calculates the angle between this entangled point and the [0X] axis. To do this, it normalizes y to normal mathematical coordinates (y rises when its value increases, not down), therefore self.center.y - point.y, and not vice versa.
Finally, in touchsMoved:
- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event {
UITouch * touch = [touches anyObject] ;
CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:self.superview]] ;
CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:self.superview]] ;
CGFloat tranA = ::atan2(self.transform.b, self.transform.a) ;
CGFloat diffA = currA - prevA ;
CGFloat angle = tranA - diffA ;
CGAffineTransform t = ::CGAffineTransformMakeRotation(angle) ;
self.transform = t ;
[self setNeedsDisplay] ;
}
verec source
share