iOS,随机滑动动画
iOS, random swipe with animation
如何制作带有动画的随机滑动。现在它正在滑动,但滑动时没有出现新图像。
- (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];
}];
}
SwipeToTheLeft
- (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 commitAnimations];
行:
^{
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationDuration:1.0f];
[self.myImage setImage:[UIImage imageNamed:[images objectAtIndex:quoteIndex]]];
[self.view setNeedsUpdateConstraints];
[UIView commitAnimations];
}
我认为您不应该像现在这样将基于 UIView 块的动画 (animateWithDuration
) 与旧式 beingAnimations
/commitAnimations
动画混合使用。尝试摆脱 beingAnimations
/commitAnimations
调用并使用块动画的持续时间。另外,我不确定这是否重要,但我总是在我的动画块中调用 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 文档所述,只有在切换视图时才有效。由于您没有切换视图,因此它不会执行任何操作。
还有,什么是属性self.myImage
?是当前在视图层次结构中的图像视图吗?如果是这样,“myImage”就是一个容易混淆的名称。它应该是“myImageView”。图像和图像视图是完全不同的,就像视图和视图控制器是完全不同的一样。
如何制作带有动画的随机滑动。现在它正在滑动,但滑动时没有出现新图像。
- (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];
}];
}
SwipeToTheLeft
- (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 commitAnimations];
行:
^{
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationDuration:1.0f];
[self.myImage setImage:[UIImage imageNamed:[images objectAtIndex:quoteIndex]]];
[self.view setNeedsUpdateConstraints];
[UIView commitAnimations];
}
我认为您不应该像现在这样将基于 UIView 块的动画 (animateWithDuration
) 与旧式 beingAnimations
/commitAnimations
动画混合使用。尝试摆脱 beingAnimations
/commitAnimations
调用并使用块动画的持续时间。另外,我不确定这是否重要,但我总是在我的动画块中调用 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 文档所述,只有在切换视图时才有效。由于您没有切换视图,因此它不会执行任何操作。
还有,什么是属性self.myImage
?是当前在视图层次结构中的图像视图吗?如果是这样,“myImage”就是一个容易混淆的名称。它应该是“myImageView”。图像和图像视图是完全不同的,就像视图和视图控制器是完全不同的一样。