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 に答える