Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did struggle a bit with how to get a touch driven rotation, even more so because I want 100% understanding of the code I am using. So I ended up, after many failed attempts, with this:</p> <pre><code>- (CGFloat) pointToAngleFromCenter: (CGPoint) point { // transform point to a self.center'ed origin based coordinate system point.x = point.x - self.center.x ; // ditto for y, but compensate for y going downwards to y going upwards point.y = self.center.y - point.y ; return ::atan2(point.y, point.x) ; } </code></pre> <p>If anyone has a better name for this method, I'm all ears.</p> <p>What it does is that it takes a point in parent view coordinates, remaps it relative to the center of the view (which is in parent view coordinate), and computes the angle between this remapped point and the axis [0X]. To do so, it normalizes y to the normal mathematical coordinates (y goes up when its value increases, not down), hence <code>self.center.y - point.y</code> and not the opposite.</p> <p>Finally, in touchesMoved:</p> <pre><code>- (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]] ; // the current value of the rotation angle CGFloat tranA = ::atan2(self.transform.b, self.transform.a) ; // the angle difference between last touch and the current touch CGFloat diffA = currA - prevA ; // the new angle resulting from applying the difference CGFloat angle = tranA - diffA ; CGAffineTransform t = ::CGAffineTransformMakeRotation(angle) ; self.transform = t ; [self setNeedsDisplay] ; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload