JKMania
JKMania

Reputation: 239

UIView Rotation

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.

Upvotes: 1

Views: 5584

Answers (3)

visakh7
visakh7

Reputation: 26390

rotating a view on touch may help you

Upvotes: 1

Erik S
Erik S

Reputation: 1929

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.

Upvotes: 6

pradeepa
pradeepa

Reputation: 4164

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];
}

Upvotes: 8

Related Questions