根据UIPanGestureRecognizer转换值更改UIImageView中的图像

时间:2012-12-14 15:02:35

标签: ios xcode uipangesturerecognizer

我在阵列中有8个图像,UIImageView一次显示数组中的一个图像。我在UIPanGestureRecognizer上也有UIImageView所以当用户将手指侧向移动到图像上时,它会发生变化(类似于图像顶部的不可见滚动条)。我正在使用平移手势的翻译值来更改图像。这一切都有效,但不是我想要的方式。图像变化太快,只需短手指平移滚动图像。有没有办法要求更长的平移来更改图像?或者是否有其他方法可以让用户“更顺畅”?

·H

@interface FirstViewController : UIViewController
    @property (nonatomic, retain) IBOutlet UIImageView *imageView;
    - (IBAction)pan:(UIGestureRecognizer *)panGesture;

的.m

@interface FirstViewController () {
    NSArray * imageArray;
    int _currentIndex;
}

@end

@implementation FirstViewController

- (IBAction)pan:(UIPanGestureRecognizer *)panGesture {
  //  NSLog(@"panned");
    CGPoint translation = [panGesture translationInView:self.view];
    [self.panLabel setText: NSStringFromCGPoint(translation)];
    NSLog(@"%@ translation", NSStringFromCGPoint(translation));

    if (translation.x<-1) {
        _currentIndex=(_currentIndex<=0)?0:_currentIndex-1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
    }
    else if(translation.x>1){
        _currentIndex=(_currentIndex>=7)?7:_currentIndex+1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    imageArray = [[NSArray alloc]
                  initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", nil];
}

1 个答案:

答案 0 :(得分:1)

您可以修改代码以跟踪X pan随时间的变化。这将允许您为平移量设置变量。修改后的代码如下:

@interface FirstViewController () {
    NSArray * imageArray;
    int _currentIndex;

    CGFloat _currentPanXAmount;
    CGFloat _panThreshold;
}

@end

@implementation FirstViewController

- (IBAction)pan:(UIPanGestureRecognizer *)panGesture {
    CGPoint translation = [panGesture translationInView:self.view];

    _currentPanXAmount += translation.x;

    [self.panLabel setText: NSStringFromCGPoint(translation)];
    NSLog(@"%@ translation", NSStringFromCGPoint(translation));

    if (_currentPanXAmount < (-1 * _panThreshold)) {
        _currentIndex=(_currentIndex<=0)?0:_currentIndex-1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
        _currentPanXAmount = 0.0;
    }
    else if(_currentPanXAmount > _panThreshold){
        _currentIndex=(_currentIndex>=7)?7:_currentIndex+1;
        UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:_currentIndex]];
        [self.imageView setImage:img];
        _currentPanXAmount = 0.0;
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    imageArray = [[NSArray alloc]
                  initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", nil];

    // Make this larger for slower transitions and smaller for faster transitions.
    _panThreshold = 10.0;
}