uiscrollview中的页面控制

时间:2011-05-18 13:12:59

标签: objective-c xcode

我创建了一个包含页面控件的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;
}

1 个答案:

答案 0 :(得分:3)

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

应该是

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