iOS,随机滑动动画

时间:2016-04-25 13:20:10

标签: 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];
                         }]; 
}

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

2 个答案:

答案 0 :(得分:1)

我认为你不应该将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"。图像和图像视图完全不同,就像视图和视图控制器完全不同。

答案 1 :(得分:0)

移动块末尾的[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];
 }