UIPageControl + UIScrollView 创建一个首尾相连页面指示器控件
来源:互联网 发布:java ivy是什么 编辑:程序博客网 时间:2024/05/20 20:48
上一篇文章里面已经讲到怎么创建一个页面指示器控件了,今天这篇文章是创建一个首尾相连的页面指示器控件,
代码:
#define BASEHEIGHT 284.0f
#define NPAGES 5 // works better with 5 or more. will work with 3 minimum
#define BIGNUM 500
@interface TestBedViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIPageControl *pageControl;
UIScrollView *sv;
int cpage;
}
@end
@implementation TestBedViewController
- (void) updatePagePlacement
{
CGPoint offset = sv.contentOffset;
// Current Page
UIView *v = [sv viewWithTag:900 + cpage];
CGRect newframe = CGRectMake(offset.x, 0.0f, 320.0f, BASEHEIGHT);
if (!CGRectEqualToRect(newframe, v.frame)) v.frame = newframe;
// printf("Center: %d\n", cpage);
// Pages to the left
int half = (NPAGES / 2);
float dx = -320.0f * half;
int i = 0;
for ( i;i < half; i++)
{
int tag = (cpage + i + NPAGES - half) % NPAGES;
// printf("Left: %d\n", tag);
UIView *v = [sv viewWithTag:900 + tag];
CGRect newframe = CGRectMake(offset.x + dx, 0.0f, 320.0f, BASEHEIGHT);
if (!CGRectEqualToRect(newframe, v.frame)) v.frame = newframe;
dx += 320.0f;
}
// Pages to the right
int nleft = NPAGES - half;
dx = 320.0f;
int ii = 1;
for (ii ;ii < nleft; ii++)
{
int tag = (cpage + ii + NPAGES) % NPAGES;
// printf("Right: %d\n", tag);
UIView *v = [sv viewWithTag:900 + tag];
CGRect newframe = CGRectMake(offset.x + dx, 0.0f, 320.0f, BASEHEIGHT);
if (!CGRectEqualToRect(newframe, v.frame)) v.frame = newframe;
dx += 320.0f;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGPoint offset = sv.contentOffset;
int thispage = (int)(offset.x / 320.0f) % NPAGES;
[self updatePagePlacement];
cpage = thispage;
}
- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
CGPoint offset = sv.contentOffset;
cpage = (int)(offset.x / 320.0f) % NPAGES;
pageControl.currentPage = cpage;
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.title = @"Image Scroller";
// Create the scroll view and set its content size and delegate
sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
sv.contentSize = CGSizeMake(BIGNUM * 2 * NPAGES * 320.0f, sv.frame.size.height);
sv.contentOffset = CGPointMake(320.0f * BIGNUM * NPAGES, 0.0f);
sv.pagingEnabled = YES;
sv.showsHorizontalScrollIndicator = NO;
sv.delegate = self;
int i = 0;
// Load in all the pages
for ( i;i < NPAGES; i++)
{
NSString *filename = [NSString stringWithFormat:@"image%d.png", ((i % 3) +1)];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
iv.frame = CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT);
iv.tag = 900 + i;
[sv addSubview:iv];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = [NSString stringWithFormat:@"Slide %d\n", i + 1];
label.backgroundColor = [UIColor blackColor];
label.textColor = [UIColor whiteColor];
[label sizeToFit];
label.center = iv.center;
[iv addSubview:label];
[label release];
[iv release];
}
cpage = 0;
[self updatePagePlacement];
[self.view addSubview:sv];
pageControl.numberOfPages = NPAGES;
pageControl.currentPage = 0;
pageControl.enabled = NO;
}
@end
- UIPageControl + UIScrollView 创建一个首尾相连页面指示器控件
- UIPageControl + UIScrollView 创建一个页面指示器控件
- UIScrollView & UIPageControl 的创建
- iOS开发之 滚动视图 + 页面控件(UIScrollView + UIPageControl)
- UIPageControl-页面控件
- 【IOS-DEMO】UIScrollView+UIPageControl 实现欢迎页面
- UIScrollView 和 UIPageControl实现页面循环播放
- UIScrollView 按页面滚动视图 UIPageControl
- UIScrollView(滚动视图)/UIPageControl(分页控件)
- UIScrollView、UIPageControl
- UIScrollView--UIPageControl
- UIScrollView、UIPageControl
- UIPageControl+UIScrollView
- UIScrollView,UIPageControl
- UIScrollView+UIPageControl
- UIScrollView、UIPageControl
- UIScrollView&UIPageControl
- UIScrollView/UIPageControl/
- 值得关注的IT技术博客
- SQL SERVER 函数中文文档
- OpenSL 播放声音
- 免费电信wifi
- 将字符串转换成十进制数字
- UIPageControl + UIScrollView 创建一个首尾相连页面指示器控件
- 串口数据在示波器上高地位是反的!!!!!!!!!!!!!!!!!!
- Linux系统优化
- 菜 鸟 变 黑 客 终 极 教 程
- 双系统重装win7/Xp后如何恢复ubuntu引导
- VC ado连接数据库
- Android图片处理(Matrix,ColorMatrix)
- apache的工作模式介绍
- iphone沙盒