gpt4 book ai didi

iphone - 如何终止 iPhone-OS 中正在运行的动画?

转载 作者:行者123 更新时间:2023-12-03 19:31:49 26 4
gpt4 key购买 nike

我有一个“Cube”类,其中有一个方法 -rotate。该方法将anchorPoint设置在左下角,然后将立方体旋转90度。之后,调用另一个方法,使立方体向后旋转-90度。指定停止选择器就完成了。顺便说一句:这没有特殊目的。刚刚学习!

-rotate 在触摸事件时被调用。但现在,当立方体对象仍在旋转时,假设它当前处于 33.943...,用户可能会在该动画完成之前发出另一个触摸事件。

此触摸事件将立即再次调用 -rotate 方法。发生的情况是,变换重叠,立方体图像完全拉伸(stretch)和变形。

因此,在新动画开始之前,当前正在运行的动画必须停止。

更新

在开始动画之前我称之为:

- (void)resetAnimation {
self.layer.transform = CATransform3DIdentity; // reset the transform to the identity transform ("no rotation applied, please")
[Cube setAnimationDelegate:nil];
[Cube setAnimationDidStopSelector:NULL];
}

另外,我设置了 [Cube setAnimationBeginsFromCurrentState:YES];在我开始动画 block 的所有地方。

因此,当从外部启动动画时,将如下所示:(注意,屏幕上有 5 个此类对象,可以将其踢出动画)

- (void)kickAnimation { // if that's called, the cube gets a kick to rotate
self.layer.transform = CATransform3DIdentity;
currentDegrees = 90; // just testing! it may also be negative
NSString *animID = [NSString stringWithFormat:@"cube_%i",self.datasetID];
if (currentDegrees > 0) {
[self rotateRight:animID finished:0 context:self];
} else if (currentDegrees < 0) {
[self rotateLeft:animID finished:0 context:self];
}
}

- (void)rotateRight:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// 1) here the anchorPoint is set to the lower right corner
// 2) here the view's frame is repositioned so that the cube doesn't move visually after the anchorPoint changed
[self rotateByDegrees:currentDegrees animationID:animationID];
}

- (void)rotateByDegrees:(CGFloat)degrees animationID:(NSString *)animationID {
[Cube beginAnimations:animationID context:self];
[Cube setAnimationDelegate:self];
[Cube setAnimationDidStopSelector:@selector(rotateBack:finished:context:)];
[Cube setAnimationDuration:2.1f];
[Cube setAnimationBeginsFromCurrentState:YES];
CATransform3D rotatedTransform = self.layer.transform;
rotatedTransform = CATransform3DRotate(rotatedTransform, degrees * M_PI / 180.0, 0.0f, 0.0f, 1.0f);
self.layer.transform = rotatedTransform;
[UIView commitAnimations];
}

- (void)rotateBack:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
currentDegrees *= -1;
[Cube beginAnimations:animationID context:self];
self.layer.transform = CATransform3DIdentity;
[Cube setAnimationDelegate:nil];
[Cube setAnimationDidStopSelector:NULL];
[Cube setAnimationDuration:2.1f];
[Cube setAnimationBeginsFromCurrentState:YES];
CATransform3D rotatedTransform = self.layer.transform;
rotatedTransform = CATransform3DRotate(rotatedTransform, currentDegrees * M_PI / 180.0, 0.0f, 0.0f, 1.0f);
self.layer.transform = rotatedTransform;
currentDegrees = 0;
[UIView commitAnimations];
}

现在看来几乎可以工作了。如果动画开始重叠,立方体不再变形。但有一件事仍然是错误的:如果有新的“踢”进来,并且立方体当前正在向后旋转,它应该立即开始向右旋转。但碰巧它首先旋转回来,然后识别出“踢”,这看起来很奇怪;)

最佳答案

Here是一个类似的问题和答案。

如果您调用[UIView setAnimationBeginsFromCurrentState:YES];,新的动画将从 View 所在的位置开始并停止先前的动画。

关于iphone - 如何终止 iPhone-OS 中正在运行的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/874679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com