2017-05-22 27 views
0

我是一個iOS開發新手,我有一個UIImageView,我想要實現fling功能,當用戶在屏幕上閃動時,頁面可以快速上下移動。如何使用Objective C在ImageView上實現fing功能?

現在我認爲是添加兩個UISwipeGesturesRecognizers,但我不知道下一步應該怎麼做,我應該使用一個分析來做到這一點,以及如何計算分析距離和時間?

另外我找到其他答案說不需要手勢識別器可以使用滾動視圖,我完全困惑,有沒有我可以學習的任何示例?謝謝你advace!

+0

您正在尋找類似的東西:https://github.com/nicklockwood/iCarousel? –

+0

我很抱歉不,我想上下跳動。 – newszer

+0

你也可以使用這個 –

回答

0

你可以那樣做:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

[scrollView setPagingEnabled:YES]; 
[scrollView setAlwaysBounceVertical:YES]; 

NSArray *imagesArray = [NSArray arrayWithObjects:@"img1.png", @"img2.png", @"img3.png", nil]; 

for (int i = 0; i < [imagesArray count]; i++) 
{ 
    CGFloat xOrigin = i * scrollView.frame.size.width; 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)]; 
    [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]]; 
    [imageView setContentMode:UIViewContentModeScaleAspectFit]; 

    [scrollView addSubview:imageView]; 
} 
    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height*[imagesArray count])]; 
+0

向上和向下扔我有一些關於上述代碼的問題,1我應該在哪裏添加這些代碼,使用方法或類?以及imagesArray用於什麼,我不需要這個數組。 – newszer