如何使用动画进行随机滑动。现在它正在滑动,但滑动时没有出现新图像。
- (void)swipeToTheRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(@"Swiped to the right");
[UIView animateWithDuration:0.5
animations:^{
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationDuration:1.0f];
[UIView commitAnimations];
[self.myImage setImage:[UIImage imageNamed:[images objectAtIndex:quoteIndex]]];
[self.view setNeedsUpdateConstraints];
}];
}
向左滑动
- (void)swipeToTheLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(@"Swiped to the left");
[UIView animateWithDuration:0.5
animations:^{
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[UIView setAnimationDuration:1.0f];
[UIView commitAnimations];
[self.myImage setImage:[UIImage imageNamed:[images objectAtIndex:quoteIndex]]];
[self.view setNeedsUpdateConstraints];
}];
@end
最佳答案
我认为您不应该像您一样将基于 UIView block 的动画 (animateWithDuration
) 与旧式 beingAnimations
/commitAnimations
动画混合使用是做。尝试摆脱 beingAnimations
/commitAnimations
调用并使用 block 动画的持续时间。另外,我不确定这是否重要,但我总是在我的动画 block 中调用 layoutIfNeeded,而不是 setNeedsUpdateConstraints
。
编辑:
所以你的代码应该是这样的:
- (void)swipeToTheRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(@"Swiped to the right");
[UIView animateWithDuration:0.5
animations:^
{
UIImage *anImage = [UIImage imageNamed:[images objectAtIndex:quoteIndex]];
[self.myImage setImage: anImage];
[self.view layoutIfNeeded];
}];
}
您拥有的setAnimationTransition
代码不会执行任何操作。如 setAnimationTransition
部分中的 Xcode 文档所述,这仅在您切换 View 时有效。由于您没有切换 View ,因此它不会执行任何操作。
此外,self.myImage
属性是什么?是当前在 View 层次结构中的 ImageView 吗?如果是这样,“myImage”就是一个容易混淆的名称。它应该是“myImageView”。图像和 ImageView 完全不同,就像 View 和 View Controller 完全不同一样。
关于iOS,随机滑动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36841777/