Iphone 滚动视图中的分页
_Iphone 滚动视图中的分页,iphone,Iphone,_ viewdidload { NSArray*colors=[NSArray ARRAY WITHOBJECTS:[UIColor REDCLOR]、[UIColor GREENCLOR]、[UIColor blueColor]、nil]; 对于(int i=0;i
viewdidload
{
NSArray*colors=[NSArray ARRAY WITHOBJECTS:[UIColor REDCLOR]、[UIColor GREENCLOR]、[UIColor blueColor]、nil];
对于(int i=0;i
我用这段代码在scrollview中用不同的颜色显示分页,为了识别,我只想用图像代替颜色
viewdidload
{
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator 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;
}
}
NSArray*colors=[NSArray阵列WithObjects:[UIImage ImageName:@]h1@2x“],[UIImage ImageName:@”h2@2x“],[UIImage ImageName:@”h2。1@2x“],无];
对于(int i=0;i
但是我只有两个视图,图像大小不适合它是去定位的。如何用上面的代码设置图像。请帮助我解决这个问题。
提前谢谢
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"h1@2x"], [UIImage imageNamed:@"h2@2x"], [UIImage imageNamed:@"h2.1@2x"], nil];
for (int i = 0; i < colors.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 = [[UIView alloc] initWithFrame:frame];
UIImage *imggg = [colors objectAtIndex:i];
[subview setBackgroundColor:[UIColor colorWithPatternImage:imggg]];
[self.scrollView addSubview:subview];
[subview release];
}
这看起来像是您的代码正在尝试执行的操作
如果您希望在每个页面上显示图像,请执行以下操作:
UIView *subview = [[[UIView alloc] initWithFrame:frame] autorelease];
[subview setBackgroundColor:[colors objectAtIndex:i]];
[[self scrollView] addSubview:subview];
要为每个页面创建多个图像,只需为页面使用UIImages数组
UIImage *img = [UIImage imageNamed:@"nameOfImageFile"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
[[self scrollView] addSubview:imageView];
我通过将数组中图像的大小减小到scrollview的大小来得到答案。我的scrollview大小是320424,我将图像的大小切片为320424。它工作得很好。谢谢。
#导入
NSArray *imageNames = [NSArray arrayWithObjects:@"image1Name", @"image2name", @"image3Name", nil];
UIImage *img = [UIImage imageNamed:[imageNames objectAtIndex:pageNumber]];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
[[self scrollView] addSubview:imageView];
@界面ViewController:UIViewController
{
IBUIScrollView*滚动视图;
}
@结束
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
NSArray*colors=[NSArray ARRAY WITHOBJECTS:[UIImage IMAGENAME:@“chiranjeevi.jpeg”]、[UIImage IMAGENAME:@“AR.jpeg”]、[UIImage IMAGENAME:@“sachin.jpeg”]、nil];
scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*colors.count,scrollView.frame.size.height);
对于(int i=0;i),但在上面的代码中,我只获得@“nameOfImageFile”一个图像,但我在每个页面上都有多个图像要隐藏。有关多个图像的实现,请参阅“添加”。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UIScrollView *scrollView;
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:@"chiranjeevi.jpeg"], [UIImage imageNamed:@"AR.jpeg"], [UIImage imageNamed:@"sachin.jpeg"], nil];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*colors.count,scrollView.frame.size.height);
for (int i = 0; i <[colors count]; i++)
{
UIImage *imggg = [colors objectAtIndex:i];
CGRect frame;
frame.origin.x =scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image=imggg;
[scrollView addSubview:subview];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end