UIView Rotation

2019-04-15 22:59发布

问题:

Hello All I want to rotate UIView on single finger touch and it still rotate untill finger moves on screen of iPhone and it stops rotation when I stop the finger moving or remove it from screen.

Thanks in Advance.

回答1:

Try similar code

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    CGAffineTransform rr=CGAffineTransformMakeRotation(5);
    yourView.transform=CGAffineTransformConcat(yourView.transform, rr);
    [UIView commitAnimations];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesBegan:touches withEvent:event];
}


回答2:

Pradeepa's code is nice, however, that animation system is getting deprecated (if not already). Try something like this instead:

CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
double startRotationValue = [[[yourView.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] doubleValue];
rotation.fromValue = [NSNumber numberWithDouble:startRotationValue];
rotation.toValue = [NSNumber numberWithDouble:startRotationValue+5];
rotation.duration = 0.2;
[yourView.layer addAnimation:rotation forKey:@"rotating"];

I took Pradeepa's numbers to make them comparable, but i believe Apple prefers you using this or block-based animation instead of the old system.



回答3:

rotating a view on touch may help you