iOS,随机滑动动画

标签 ios animation random swipe

如何使用动画进行随机滑动。现在它正在滑动,但滑动时没有出现新图像。

- (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/

相关文章:

iPhone iOS 如何向按钮文本或图像下的 UIButton 添加线性渐变?

ios - 如何在单独的动画中将 CGAffineTransform 链接在一起?

wpf - 使用 wpf VisualStateManager 动画可见性和不透明度

javascript - 如何制作不出错的透明 Canvas ?

c++ - 为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行都会得到相同的序列?

c++ - 来自函数的随机整数总是返回相同的数字 - 为什么?

ios - 定向光的阴影甚至在SceneKit中也可以工作吗?

iOS打开谷歌地图路线

ios - 更新 CoreData 添加了很多 nil 值

r - 来自 R 中 Erlang 分布的样本