The rotation always starts from the starting position

I am trying to implement a wheel type function. I calculate the center point of the view and find the angle created by moving in the touches Moved: method. For the first move, he turned slightly. But for the next move, it returns to the original view position and then rotates. In general, I want to rotate from the end point of the previous rotation. Anything missing? Any help

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    float a, b,c;   

    UITouch *aTouch = [touches anyObject];
    if ([aTouch view] == dialView) {

        CGPoint loc = [aTouch locationInView:dialView];
        CGPoint prevloc = [aTouch previousLocationInView:dialView];

        c = [self GetSides:prevloc point2:loc]; 
        a = [self GetSides:loc point2:cntrePoint];
        b = [self GetSides:prevloc point2:cntrePoint];

        float angle = acos((a*a+b*b-c*c)/(2*a*b));// Calculating angle created on moving

        CABasicAnimation  *rotate ;
        rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotate.fromValue = [NSNumber numberWithFloat:0];
        rotate.toValue = [NSNumber numberWithFloat:((angle*M_PI)/180)];
        rotate.autoreverses = NO;
        rotate.fillMode = kCAFillModeForwards;
        rotate.duration = 1;
        rotate.repeatCount = 1;
        rotate.removedOnCompletion = NO;
        rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

        [dialView.layer addAnimation:rotate forKey:@"360"];
    }
}

      

+1


source to share


1 answer


The animation you are doing that looks like drawn from this answer has both specified and specified values, so it will simply rotate from 0 to a radian angle every time. This means that your image will go back to the beginning of each animation.

Usually, if you must remove the fromValue line, your animation should go from the current corner to your new corner, but it looks like the transform property behaves a little differently. You will need to set the fromValue to the current value of the rotational transform retrieved from your presentationLayer. The following code should rotate with a target angle from the current angle:

CABasicAnimation  *rotate;
rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotate.fromValue = [[dialView.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"];
rotate.toValue = [NSNumber numberWithFloat:angle];
rotate.fillMode = kCAFillModeForwards;
rotate.duration = 1;
rotate.removedOnCompletion = NO;
rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

      



Note that I have removed the conversion of degrees to radians in toValue because your acos () calculation returns values ​​in radians.

EDIT (10/23/2009): Corrected my previous assumption about the function of fromValue with the transform property.

+8


source







All Articles