【问题标题】:How do I create a fade in effect from one image to another on UIImageView?如何在 UIImageView 上创建从一个图像到另一个图像的淡入效果?
【发布时间】:2014-08-22 13:29:39
【问题描述】:

您好,我有以下代码试图为我的 imageview 淡入淡出图像:

- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer {
// Insert your own code to handle swipe left
[imageview setAlpha:0];
[UIImageView beginAnimations:NULL context:nil];
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIImageView setAnimationDuration:1];
[imageview setAlpha:1];
[UIImageView commitAnimations];
imageview.image = [UIImage imageNamed:@"image1.jpg"];
}

- (void)oneFingerSwipeRight:(UITapGestureRecognizer *)recognizer {
// Insert your own code to handle swipe right
[imageview setAlpha:0];
[UIImageView beginAnimations:NULL context:nil];
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIImageView setAnimationDuration:1];
[imageview setAlpha:1];
[UIImageView commitAnimations];
imageview.image = [UIImage imageNamed:@"image2.jpg"];

但是,这会以某种方式使图像闪烁,我如何使从一个图像到另一个图像的平滑淡入/淡出过渡?

我还有名为 image1、image2、image3 等的图像。 每次滑动时如何从图像 1 转换到 2 到 3?

【问题讨论】:

    标签: ios animation uiimageview uiviewanimationtransition


    【解决方案1】:

    首先,不要使用“旧式”动画。来自Apple docs

    不鼓励在 iOS 4.0 及更高版本中使用此方法。你应该使用 使用基于块的动画方法来指定您的动画。

    要在 2 个图像视图之间淡入淡出,请尝试以下操作:

    @property (strong, nonatomic) UIImageView * myImageView;
    
    - (void) fadeToImage: (UIImage *) newImage
    {
        // First create a new imageView in the same place as the current one
        UIImageView * newImageView = [[UIImageView alloc] initWithFrame: self.myImageView.frame];
        newImageView.image = newImage;
        newImageView.alpha = 0.0;
        [self.myImageView.superview insertSubview: newImageView aboveSubview: self.myImageView];
    
        [UIView animateWithDuration: 1.0
                         animations:^{
    
                             // Cross-fade the images
                             newImageView.alpha = 1.0;
                             self.myImageView.alpha = 0.0;
                         }
                         completion:^(BOOL finished) {
                             // Finally remove the old imageView and replace with the new
                             [self.imageView removeFromSuperview];
                             self.myImageView = newImageView;
                         }];
    }
    

    【讨论】:

      【解决方案2】:

      进行适当的交叉溶解需要更多的工作,但并不多。首先,您需要三个视图,一个父视图和两个子视图。父视图可以是通用的UIView(在下面的代码中,我使用视图控制器的主视图作为父视图)。这两个孩子是UIImageViews,是父视图的子视图。子代应该具有相同的frames,但子代不必与父代大小相同。

      最初,currentImageView 应将其hidden 属性设置为NOnextImageView 应将其hidden 属性设置为YES。转换代码如下所示。

      [UIView transitionWithView:self.view
                        duration:1.0
                         options:UIViewAnimationOptionTransitionCrossDissolve
                      animations:^{
                          self.nextImageView.hidden = NO;
                          self.currentImageView.hidden = YES;
                      }
                      completion:^(BOOL finished) {
                          UIImageView *temp = self.currentImageView;
                          self.currentImageView = self.nextImageView;
                          self.nextImageView = temp;
                      }
      ];
      

      completion 块中的指针交换允许您通过更改self.nextImageViewimage 属性来执行下一个转换,然后再次运行转换。

      【讨论】:

        猜你喜欢
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 2012-12-29
        • 2013-07-15
        • 2013-07-01
        • 2016-04-16
        • 2012-01-25
        • 1970-01-01
        相关资源
        最近更新 更多