UIView轮换

JKM*_*nia 1 iphone objective-c uiview cgaffinetransform

你好所有我想用单指触摸旋转UIView它仍然旋转直到iPhone屏幕上的手指移动,当我停止手指移动或从屏幕上移除它时它停止旋转.

提前致谢.

pra*_*epa 8

尝试类似的代码

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


Eri*_*k S 6

然而,Pradeepa的代码很不错,动画系统已被弃用(如果还没有).尝试这样的事情:

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"];
Run Code Online (Sandbox Code Playgroud)

我采用了Pradeepa的数字来使它们具有可比性,但我相信Apple更喜欢使用这个或基于块的动画而不是旧系统.