Rotation always start from original position

2019-07-24 11:19发布

问题:

I am trying to implement a wheel type functionality. I calculate the center point of the view and find the angle created on moving in touchesMoved: method. For the first move it rotated by some degree. But for next move it comes back to the original position of the view and then rotating.Actually i want the rotation from the end point of the previous rotation. Any thing 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:

The animation you are performing, which looks to be drawn from this answer, has both the to and from values specified, so it will simply rotate from 0 to angle radians every time. That means that your image will jump back to the start on every animation.

Normally, if you were to remove the fromValue line, your animation should go from the current angle to your new angle, but it looks like the transform property behaves a little differently. You'll need to set the fromValue to be the current value of the rotational transform extracted from your layer's presentationLayer. The following code should rotate to the target angle from your 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've removed the degrees-to-radians conversion in the toValue, because your acos() calculation returns values in radians.

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