2013-11-03 61 views
1

我試圖使用圖像數組實現頁面控制和滾動視圖,出於某種原因,當滑動到下一頁時,圖像正在轉移。它幾乎看起來像圖像對於滾動視圖來說太大了,但是使它們變小似乎並不奏效。有沒有辦法讓ScrollView不垂直滾動?見下面的代碼。圖像大小不能與頁面控制和滾動視圖一起使用?

enter image description here

SSViewController.h

#import <UIKit/UIKit.h> 

@interface SSViewController : UIViewController <UIScrollViewDelegate> 

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl; 


@property (nonatomic, strong) NSArray *imageArray; 

SSViewController.m

-(void)viewDidAppear:(BOOL)animated 
{ 

    imageArray = [[NSArray alloc] initWithObjects:@"first.png", @"second.png", nil]; 

for (int i = 0; i < [imageArray count]; i++) { 
    //We'll create an imageView object in every 'page' of our scrollView. 
    CGRect frame; 
    frame.origin.x = self.scrollView.frame.size.width * i; 
    frame.origin.y = 0; 
    frame.size = self.scrollView.frame.size; 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame]; 
    imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]]; 
    size:imageView.clipsToBounds = YES; 
    imageView.contentMode = UIViewContentModeScaleAspectFill; 

    [self.scrollView addSubview:imageView]; 
} 
//Set the content size of our scrollview according to the total width of our imageView objects. 
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height); 
} 


    - (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; 
     self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0); 

    } 

回答

1

我覺得好主意,會改變你的所有allocaton代碼(espacially所有代碼定義幀)到viewWillAppear方法,因爲在viewDidLoad某些幀不會被設置。

+0

將代碼移到viewWillAppear(請參閱上面編輯的代碼),但我仍然有同樣的問題。 –

+1

當你設置圖像到你的imageView添加這兩行後縮放圖像到imageView框架大小:imageView.clipsToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFill; – edzio27

+0

添加了這兩行(參見上面的代碼),沒有工作:( –

相關問題