多个图像滑动手势

时间:2016-01-29 18:21:16

标签: ios objective-c uiimageview uiswipegesturerecognizer

我在本教程后面创建了图像Swipe Gesture:the tutorial

图像swiper工作正常但我需要你的帮助 - 如何复制此代码以制作更多“swipers”,具有不同的图像。 有没有编码的简单方法可以做到这一点?

代码是:(viewcontroller.m)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
NSInteger imageindex = 2;

- (IBAction)handleswipe:(UIGestureRecognizer *)sender {
NSLog( @"swiped");
NSArray *images=[[NSArray alloc] initWithObjects:
                 @"vodkacomp1.jpg",
                 @"vodkacomp2.jpg", 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];
_vodkacom.image = [UIImage imageNamed:[images objectAtIndex:imageindex]];    
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

viewcontroller.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *vodkacom;

- (IBAction)handleswipe:(UIGestureRecognizer *) sender;


@end

1 个答案:

答案 0 :(得分:0)

我认为没有编码可以做到这一点,但你可以使用UIScrollView的分页功能来实现你想要的东西,Apple在这里有一页示例代码:PageControl