[英]How to add UIPageControl in UIScrollView
This is my UIScrollView
function, i displayed 7 images out of 39 in one page. 这是我的UIScrollView
函数,我在一页中显示了39张图像中的7张。 It works but I want to display and change the UIPageControl
. 它可以工作,但是我想显示和更改UIPageControl
。
- (void)layoutScrollImages{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
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)];
}
- (void)viewDidLoad {
kScrollObjHeight = 172.0;
kScrollObjWidth = 146.0;
kNumImages = 39;
scrollView1=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 596, 1022, 172)];
scrollView1.showsVerticalScrollIndicator=YES;
scrollView1.userInteractionEnabled=YES;
[self.view addSubview:scrollView1];
self.view.backgroundColor = [UIColor whiteColor];
[scrollView1 setBackgroundColor:[UIColor whiteColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = NO;
scrollView1.scrollEnabled = YES;
scrollView1.pagingEnabled = YES;
NSUInteger j;
for (j = 1; j <= kNumImages; j++)
{
NSString *imageName = [NSString stringWithFormat:@"thumb_%lu.png",(unsigned long)j];
image = [UIImage imageNamed:imageName];
imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = j; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
}
pageCtrl = [[UIPageControl alloc] init];
pageCtrl.frame = CGRectMake(-130, CGRectGetMidY(self.view.bounds)+25, 1024, 100);
[pageCtrl setNumberOfPages:5];
pageCtrl.currentPage = 0;
pageCtrl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageCtrl.currentPageIndicatorTintColor = [UIColor blackColor];
[self.view addSubview:pageCtrl];
}
How can I use UIPageControl here? 如何在这里使用UIPageControl?
Add Target for Page control like as follows, 为页面控件添加目标,如下所示,
[pageControl addTarget:self action:@selector(onPageControlPageChanged:) forControlEvents:UIControlEventValueChanged];
then, 然后,
- (void)onPageControlPageChanged:(UIPageControl *)pageControl_ {
int offsetX = pageControl_.currentPage * kScrollObjWidth;
CGPoint offset = CGPointMake(offsetX, 0);
[scrollView setContentOffset:offset animated:YES];
}
. 。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView_ {
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
pageCtrl.currentPage = page;
}
at - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
在- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
You count currentPage and set like 您计算currentPage并设置为
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
pageCtrl.currentPage = (int)(self.scrollView.contentOffset.x / kScrollObjWidth);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.