iphone - How do I keep track of the total number of complete rotations of an image in an iOS app? -
iphone - How do I keep track of the total number of complete rotations of an image in an iOS app? -
i working on app has rotating image (the user tapps , drags , image rotates in circle tracking finger). trying maintain track of how many times user makes finish circle. additional "hitch" need know if user circling clockwise vs counter clockwise.
here code rotating image... please sense free request additional information.
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event{ uitouch *touch = [[event alltouches] anyobject]; cgpoint touchpoint = [touch locationinview:self.view]; long double rotationnumber = atan2(touchpoint.y - originy, touchpoint.x - originx); totalrotationcount ++; schedulingwheel.transform = cgaffinetransformmakerotation(rotationnumber); offset = (rotationnumber * 100)/14; dateribbon.center = cgpointmake(offset, 24); }
thanks help!
my solution isn't elegant , there might cleaner solution i'm missing did recently. trick maintain track of angle lastly time touchesmoved:
called. then, add together delta of current angle , stored angel value total.
the problem "boundaries" atan2 creates needed ugly code overcome. lastangle
359 , cross origin next angle 1. difference not 2 -358, whenever cross boundary total reset 0.
here did:
cgfloat angle = atan2f(center.y - point.y, point.x - center.x); //translate unit circle if (angle > 0) { angle = (m_pi - angle) + m_pi; } else { angle = fabsf(angle); } cgfloat delta = angle - lastangle; //adjust boundaries if (fabsf(delta) > (2*m_pi)-fabsf(delta)) { bool greaterthanzero = (delta > 0); delta = (2*m_pi)-fabsf(delta); if (greaterthanzero) { delta = -1 * delta; } } totalangle += delta; lastangle = angle;
the big/ugly conditional under "adjust boundaries" looks see if there shorter angle new point (so, 2 instead of -258) & assumes if there means crossed origin , adjusts delta accordingly.
i translated atan2 results represents total unit circle 0 2π. bonus side affect, accounts clockwise/counter clockwise motion improve standard -π π of atan2.
iphone ios
Comments
Post a Comment