2012-05-06 49 views

回答

8

常用的方法是UIScrollView其中一個圖像的大小,啓用分頁功能。

添加圖像作爲子視圖並設置內容的大小是這樣的...

NSArray *images; 
CGSize imageSize; 

self.scrollView.frame = CGRectMake(10,10,imageSize.width,imageSize.height); 
self.scrollView.contentSize = CGSizeMake(imageSize.width * images.count, imageSize.height); 
self.scrollView.pagingEnabled = YES; 

CGFloat xPos = 0.0; 

for (UIImage *image in images) { 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.width); 
    [self.scrollView addSubview:imageView]; 
    xPos += imageSize.width; 
    // assuming ARC, otherwise release imageView 
} 

您也可能會關閉彈跳,滾動指標等,這取決於你想要的效果的細節。

+0

的IBOutlet中我也嘗試一下,不知道如果我正確地按照你的建議,它不起作用,scrollview保持空白。也許我錯過了一些東西,因爲我不瞭解對象,事件是如何工作的......謝謝 – Sweet

+0

這個答案驅使我去解決我的問題的鏈接:http://www.iosdevnotes.com/2011/03/uiscrollview-分頁/。感謝Danh,我把它投票並標記爲回答 – Sweet

1

你可以簡單地獲得刷卡方向向左或向右UISwipeGestureRecognizer這樣

swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)]; 
[swipeGesture setNumberOfTouchesRequired:1]; 
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp]; 
[appView addGestureRecognizer:swipeGesture]; 

的方法detectSwipe申報處理您的向前和向後的UIImage數組玩。您還可以查看Apple's SimpleGestureRecognizers Demo

希望它可以幫助你!

2

使用的UIScrollView和UIPageControl,拖動ñ下降的UIScrollView和UIPageControl確保它們不重疊,創造both.Set

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // 1 
imageArray = [NSArray arrayWithObjects: 
        [UIImage imageNamed:@"image2.jpg"], 
        [UIImage imageNamed:@"man-actor-Ashton-Kutcher-Wallpaper.jpg"], 
        [UIImage imageNamed:@"image1.jpg"], 
        [UIImage imageNamed:@"man-curl.jpg"], 
        [UIImage imageNamed:@"man-in-suit.jpg"], 
        nil]; 


    for (int i = 0; i < imageArray.count; i++) { 
    CGRect frame; 
    frame.origin.x = self.scrollView.frame.size.width * i; 
    frame.origin.y = 0; 
    frame.size = self.scrollView.frame.size; 

    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame]; 
    subview.image = [imageArray objectAtIndex:i]; 
    subview.contentMode = UIViewContentModeScaleAspectFit; 
    [self.scrollView addSubview:subview]; 
    } 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height); 
    pageControl.numberOfPages = imageArray.count; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    // Update the page when more than 50% of the previous/next page is visible 
    CGFloat pageWidth = self.scrollView.frame.size.width; 
    int page = floor((self.scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    self.pageControl.currentPage = page; 
} 
相關問題