张运涛

c++

   :: 首页 :: 联系 :: 聚合  :: 管理

常用链接

留言簿(4)

搜索

  •  

最新评论

1.官方Scrolling例子:
设置每页图片的位置,及UIScrollView的大小

- (void)layoutScrollImages

{

UIImageView *view = nil;

NSArray *subviews = [scrollView1 subviews];


// reposition all image subviews in a horizontal serial fashion

CGFloat curXLoc = 0;

for (view in subviews)

{

if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)

{

CGRect frame = view.frame;

frame.origin = CGPointMake(curXLoc, 0);

view.frame = frame;

curXLoc += (kScrollObjWidth);

}

}

// set the content size so it can be scrollable

[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];

}


//UIScrollView初始化,及加载图片页等

- (void)viewDidLoad

{

self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];


// 1. setup the scrollview for multiple images and add it to the view controller

//

// note: the following can be done in Interface Builder, but we show this in code for clarity

[scrollView1 setBackgroundColor:[UIColor blackColor]];

[scrollView1 setCanCancelContentTouches:NO];

scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;

scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview

scrollView1.scrollEnabled = YES;

// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo

// if you want free-flowing scroll, don't set this property.

scrollView1.pagingEnabled = YES;

// load all the images from our bundle and add them to the scroll view

NSUInteger i;

for (i = 1; i <= kNumImages; i++)

{

NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];

UIImage *image = [UIImage imageNamed:imageName];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"

CGRect rect = imageView.frame;

rect.size.height = kScrollObjHeight;

rect.size.width = kScrollObjWidth;

imageView.frame = rect;

imageView.tag = i; // tag our images for later use when we place them in serial fashion

[scrollView1 addSubview:imageView];

[imageView release];

}

scrollView1.multipleTouchEnabled=YES;

[self layoutScrollImages]; // now place the photos in serial layout within the scrollview

// 2. setup the scrollview for one image and add it to the view controller

//

[scrollView2 setBackgroundColor:[UIColor blackColor]];

[scrollView2 setCanCancelContentTouches:NO];

scrollView2.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview

scrollView2.indicatorStyle = UIScrollViewIndicatorStyleWhite;

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image0.jpg"]];

[scrollView2 addSubview:imageView];

[scrollView2 setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

[scrollView2 setScrollEnabled:YES];

[imageView release];

}


另一种方法 

// Create the scroll view and set its content size and delegate

UIScrollView *sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];

sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);

sv.pagingEnabled = YES;

sv.delegate = self;

// Load in all the pages

for (int i = 0; i < NPAGES; i++)

{

NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1];

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];

iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);

[sv addSubview:iv];

[iv release];

}

[self.view addSubview:sv];


posted on 2010-08-09 14:16 张运涛 阅读(615) 评论(0)  编辑 收藏 引用 所属分类: iphone

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理