2012-12-14 28 views
1

我有8個圖像在數組中,並且UIImageView一次顯示數組中的一個圖像。我也在UIImageView上有UIPanGestureRecognizer,所以當用戶將它們的手指在圖像上橫向移動時(它就像圖像中看不見的滾動條一樣)。我正在使用平移手勢的翻譯值來更改圖像。這一切都有效,但不是我想要的方式。圖像改變方式太快,只需要短暫的手指即可滾動瀏覽圖像。有沒有辦法需要更長的鍋來改變圖像?或者任何其他方式使用戶更「平滑」?基於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

你可以修改代碼隨着時間的推移跟蹤X鍋。這將允許您爲平移量設置一個變量。修改後的代碼如下:

@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; 
} 
+0

這正是我想要實現的。非常感謝! – finnftw

+0

關於推進圖像的方向:當從右向左平移時,從8 ---> 1開始。爲了有更直觀的「滾動」,必須顛倒「if語句」。你也錯過了我現在正在編輯的括號。 –

+0

@robhasacamera _currentIndex =(_ currentIndex> = 7)?7:_currentIndex + 1;如何翻譯這是swift2? –