1
我试图使用图像数组实现页面控制和滚动视图,出于某种原因,当滑动到下一页时,图像正在转移。它几乎看起来像图像对于滚动视图来说太大了,但是使它们变小似乎并不奏效。有没有办法让ScrollView不垂直滚动?见下面的代码。图像大小不能与页面控制和滚动视图一起使用?
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);
}
将代码移到viewWillAppear(请参阅上面编辑的代码),但我仍然有同样的问题。 –
当你设置图像到你的imageView添加这两行后缩放图像到imageView框架大小:imageView.clipsToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFill; – edzio27
添加了这两行(参见上面的代码),没有工作:( –