0

我创建了一个视图控制器,并在其中放入了一个 UIImageView,我制作了一个NSArray包含图像并传入了 的UIImageView,我添加了一个UISwipeGestureRecognizer用于向左滑动的向右滑动。

此代码有效,但图像在像没有效果或过渡的情况下像繁荣一样滑动时出现,有没有办法在它们之间添加一个简单的过渡?

这是代码:

NSArray *images = [[NSArray alloc] initWithObjects:
                 @"1-r.png",
                 @"2-r.png",
                 @"3-r.png",
                 @"4-r.png",
                 @"5-r.png",
                 nil];

UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];

switch (direction) {
    case UISwipeGestureRecognizerDirectionLeft:
        imageIndex++;
        break;
    case UISwipeGestureRecognizerDirectionRight:
        imageIndex--;
        break;

    default:
        break;
}

imageIndex = (imageIndex < 0) ? ([images count] - 1) : imageIndex % [images count];
_imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
4

1 回答 1

0

将 UIImgaView 添加到您的视图控制器并创建 IBOutled imgView。将以下代码添加到视图控制器的 .m 文件中。

- (UIImage *) prev {
    NSInteger index = [self.imgView.animationImages indexOfObject: self.imgView.image];
    if (index > 0) {
        return [self.imgView.animationImages objectAtIndex: index - 1];
    }
    return self.imgView.animationImages.lastObject;
}

- (UIImage *) next {
    NSInteger index = [self.imgView.animationImages indexOfObject: self.imgView.image];
    if (index < self.imgView.animationImages.count - 1) {
        return [self.imgView.animationImages objectAtIndex: index + 1];
    }
    return [self.imgView.animationImages objectAtIndex: 0];
}

- (void) swipeRight {
    UIImageView *topView = [[UIImageView alloc] initWithFrame: self.imgView.frame];
    topView.image = [self prev];
    topView.alpha = 0.0;
    [self.view addSubview: topView];
    [self animateImages];
}

- (void) swipeLeft {
    UIImageView *topView = [[UIImageView alloc] initWithFrame: self.imgView.frame];
    topView.image = [self next];
    topView.alpha = 0.0;
    [self.view addSubview: topView];
    [self animateImages];
}

- (void) animateImages {
    UIImageView *topView = self.view.subviews.lastObject;
    [UIView animateWithDuration: 0.5 delay: 0.0f options: UIViewAnimationOptionCurveEaseInOut animations:
     ^(void) {
         topView.alpha = 1.0;
     } completion: ^(BOOL finished) {
         self.imgView.image = topView.image;
         [topView removeFromSuperview];
     }];
}

- (void) viewDidLoad {
    NSArray *imageNames = [[NSArray alloc] initWithObjects:
             @"1-r.png",
             @"2-r.png",
             @"3-r.png",
             @"4-r.png",
             @"5-r.png",
             nil];
    NSMutableArray *images = [NSMutableArray array];
    for (NSString *filename in imageNames) {
        [images addObject: [UIImage imageNamed: filename]];
    }
    self.imgView.animationImages = images;
    self.imgView.userInteractionEnabled = YES;
    self.imgView.image = [images objectAtIndex: 0];
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector(swipeRight)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector(swipeLeft)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imgView addGestureRecognizer: leftSwipe];
    [self.imgView addGestureRecognizer: rightSwipe];
}
于 2013-09-08T13:44:27.777 回答