xcode uiscrollview 中的页面控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6045262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:03:30  来源:igfitidea点击:

page control in uiscrollview

objective-cxcode

提问by kingston

i ve created a uiscrollview containing a page control which loads the images from resource bundle..everything works good.i m able to scroll through different images..the problem is if i m to click the corresponding pagecontrol(dot), i m not able to navigate to the corresponding image....could u guys help me out below is the code...the below code works perfectly fine

我创建了一个包含从资源包加载图像的页面控件的 uiscrollview ..一切正常.我能够滚动浏览不同的图像..问题是如果我点击相应的页面控件(点),我无法导航到相应的图像....你们能帮我看看下面的代码...下面的代码工作得很好

// Email.h

@interface Email : UIViewController<UIScrollViewDelegate>
{
   UIPageControl *pageControl;
   UIScrollView *scroller;
}

@property (nonatomic,retain)IBOutlet UIPageControl *pageControl;
@property (nonatomic,retain)IBOutlet UIScrollView *scroller;

-(IBAction)clickPageControl:(id)sender;

@end


// Email.m

@implementation Email
@synthesize pageControl,scroller;

-(IBAction)clickPageControl:(id)sender
{
   int page=pageControl.currentPage;
   CGRect frame=scroller.frame;
   frame.origin.x=frame.size.width=page;
   frame.origin.y=0;
   [scroller scrollRectToVisible:frame animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   int page = scrollView.contentOffset.x/scrollView.frame.size.width;
   pageControl.currentPage=page;
}

- (void)viewDidLoad 
{
   [super viewDidLoad];
   self.title=@"Press Photos";
   for (int i=1; i<10; i++)
   {
      UIImageView *images=[[UIImageView alloc]initWithImage:
        [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
      images.frame=CGRectMake((i-1)*320, 0, 320, 460);
      [scroller addSubview:images];
      [images release];
   }
   scroller.delegate=self;
   scroller.contentSize=CGSizeMake(320*9, 460);
   scroller.pagingEnabled=YES;

   pageControl.numberOfPages=9;
   pageControl.currentPage=0;
}

采纳答案by Deepak Danduprolu

frame.origin.x=frame.size.width=page;

should be

应该

frame.origin.x = frame.size.width * page;