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];
}
這正是我想要實現的。非常感謝! – finnftw
關於推進圖像的方向:當從右向左平移時,從8 ---> 1開始。爲了有更直觀的「滾動」,必須顛倒「if語句」。你也錯過了我現在正在編輯的括號。 –
@robhasacamera _currentIndex =(_ currentIndex> = 7)?7:_currentIndex + 1;如何翻譯這是swift2? –