UIViewScroll使用

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 wecall "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];

转载于:https://www.cnblogs.com/pengyingh/articles/2432496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值