滚动视图滚动时页面控件不改变

发布于 2024-12-16 22:32:38 字数 2497 浏览 2 评论 0 原文

我正在尝试将页面控件添加到我的滚动视图中,并且遵循了许多网络教程,其中大多数使用与此相同的代码 这个,尽管答案是没有帮助。这是我的代码:

MainViewController.h

@interface MainViewController : UIViewController
{
UIScrollView *svCollegeMain;
UIScrollView *svCollegePage;
UIPageControl *pcCollege;
UIView *viewP1;
}

@property (nonatomic, retain) IBOutlet UIScrollView* svCollegeMain;
@property (nonatomic, retain) IBOutlet UIScrollView* svCollegePage;
@property (nonatomic, retain) IBOutlet UIPageControl * pcCollege;
- (IBAction)changePage;

@end

和MainViewController.m

@implementation MainViewController

@synthesize svCollegeMain, svCollegePage, pcCollege;

- (void)viewDidLoad 
{
[super viewDidLoad];
self.svCollegeMain.contentSize = CGSizeMake(960, 332);
self.svCollegePage.contentSize = CGSizeMake(320, 500);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = 320;
int page = floor((svCollegeMain.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pcCollege.currentPage = page;
}

- (IBAction)changePage 
{
CGRect frame;
frame.origin.x = self.svCollegeMain.frame.size.width * self.pcCollege.currentPage;
frame.origin.y = 0;
frame.size = self.svCollegeMain.frame.size;
[self.svCollegeMain scrollRectToVisible:frame animated:YES];
}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
[super viewDidUnload];
self.svCollegeMain = nil;
self.svCollegePage = nil;
self.pcCollege = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

以防万一这有什么不同,我的视图设置有一个视图,然后是该视图中的主滚动视图和页面控件,另一个视图和滚动视图(彼此相邻) )在主滚动视图中,最后是第二个滚动视图中的最终视图(全部在IB中,不需要太多代码),并且所有内容都在IB中链接起来。

I am trying to add a page control to my scroll view, and have followed numerous web tutorials, the majority which use the same code as this tutorial. However, once I place the code into my project, even with me making changes to the code to try to make it work, it just doesn't. I have managed to make the code work for when the page control is pressed, however it just won't work for the page scrolling. My issue is similar to this, although the answers are of no help. Here is my code:

MainViewController.h

@interface MainViewController : UIViewController
{
UIScrollView *svCollegeMain;
UIScrollView *svCollegePage;
UIPageControl *pcCollege;
UIView *viewP1;
}

@property (nonatomic, retain) IBOutlet UIScrollView* svCollegeMain;
@property (nonatomic, retain) IBOutlet UIScrollView* svCollegePage;
@property (nonatomic, retain) IBOutlet UIPageControl * pcCollege;
- (IBAction)changePage;

@end

and MainViewController.m

@implementation MainViewController

@synthesize svCollegeMain, svCollegePage, pcCollege;

- (void)viewDidLoad 
{
[super viewDidLoad];
self.svCollegeMain.contentSize = CGSizeMake(960, 332);
self.svCollegePage.contentSize = CGSizeMake(320, 500);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = 320;
int page = floor((svCollegeMain.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pcCollege.currentPage = page;
}

- (IBAction)changePage 
{
CGRect frame;
frame.origin.x = self.svCollegeMain.frame.size.width * self.pcCollege.currentPage;
frame.origin.y = 0;
frame.size = self.svCollegeMain.frame.size;
[self.svCollegeMain scrollRectToVisible:frame animated:YES];
}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
[super viewDidUnload];
self.svCollegeMain = nil;
self.svCollegePage = nil;
self.pcCollege = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Just incase this makes any difference, my view is set out with a view, then a main scroll view and page control within this view, another view and scroll view (next to each other) within the main scroll view, and finally a final view in the second scroll view (all in IB, did not want too much code), and everything is linked up in IB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦过后 2024-12-23 22:32:38

我注意到你的 MainViewController 没有声明自己实现 UIScrollViewDelegate,所以我还假设你忘记将它设置为 IB 中滚动视图的委托(否则它不会编译)。

由于它没有定义委托,因此滚动视图不会调用您的scrollViewDidScroll函数。

蒂姆

I notice that your MainViewController doesn't declare itself as implementing UIScrollViewDelegate, so I also assume that you've forgotten to set it up as the delegate for the scroll view in IB (otherwise it wouldn't compile).

Since it has no delegate defined, the scroll view won't be calling your scrollViewDidScroll function.

Tim

梦明 2024-12-23 22:32:38

试试这个

头文件:

@interface DemoPageControlViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
BOOL pageControlUsed;
NSMutableArray *imageArray;
int  pageNumber;


}


@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *imageArray;


- (IBAction) changePage:(id)sender;

实现文件:

#import "DemoPageControlViewController.h"

@implementation DemoPageControlViewController
@synthesize pageControl, scrollView, imageArray;
- (void)viewDidLoad 
{
[super viewDidLoad];

CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;

imageArray = [[NSMutableArray alloc]init];
[imageArray addObject:@"small_one.png"];
[imageArray addObject:@"small_two.png"];
[imageArray addObject:@"small_three.png"];
[imageArray addObject:@"small_four.png"];


// add the last image to first
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:       [imageArray objectAtIndex:([imageArray count] -1)]]];
imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height);
[self.scrollView addSubview:imageView];
[imageView release]; 

for(int i = 0; i < imageArray.count; i++)
{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
    imageView.frame = CGRectMake((scrollView.frame.size.width * i ) + 320   , 0, scrollView.frame.size.width, scrollView.frame.size.height);
    [self.scrollView addSubview:imageView];
    [imageView release];
}
// add the first image to last
imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:0]]];
imageView.frame = CGRectMake(scrollView.frame.size.width * ([imageArray count]+1), 0, scrollView.frame.size.width, scrollView.frame.size.height);
[self.scrollView addSubview:imageView];
[imageView release]; 

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([imageArray count]+ 2), self.scrollView.frame.size.height);
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(scrollView.frame.size.width,0,scrollView.frame.size.width,scrollView.frame.size.height) animated:NO]; 
}

- (IBAction)changePage :(id)sender
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage ;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;             
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//pageControlUsed = NO;

NSLog(@"%f", self.scrollView.contentOffset.x);

CGFloat pageWidth = self.scrollView.frame.size.width;
//pageNumber = floor((self.scrollView.contentOffset.x - pageWidth / ([imageArray count]+2)) / pageWidth) + 1  ;
pageNumber = self.scrollView.contentOffset.x / pageWidth;

if(pageNumber ==  0)
{
    [self.scrollView scrollRectToVisible:CGRectMake((self.scrollView.frame.size.width * [imageArray count]), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO];
    pageNumber = [imageArray count];
    //self.pageControl.currentPage = pageNumber;
}
else if(pageNumber == ([imageArray count]+1))
{
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO];
    pageNumber = 1;
    //self.pageControl.currentPage = pageNumber;
}

self.pageControl.currentPage = pageNumber - 1;

  }

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  {

  }

此代码工作正常。试试这个

Try this

HeaderFile:

@interface DemoPageControlViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
BOOL pageControlUsed;
NSMutableArray *imageArray;
int  pageNumber;


}


@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *imageArray;


- (IBAction) changePage:(id)sender;

Implementation File:

#import "DemoPageControlViewController.h"

@implementation DemoPageControlViewController
@synthesize pageControl, scrollView, imageArray;
- (void)viewDidLoad 
{
[super viewDidLoad];

CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;

imageArray = [[NSMutableArray alloc]init];
[imageArray addObject:@"small_one.png"];
[imageArray addObject:@"small_two.png"];
[imageArray addObject:@"small_three.png"];
[imageArray addObject:@"small_four.png"];


// add the last image to first
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:       [imageArray objectAtIndex:([imageArray count] -1)]]];
imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height);
[self.scrollView addSubview:imageView];
[imageView release]; 

for(int i = 0; i < imageArray.count; i++)
{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
    imageView.frame = CGRectMake((scrollView.frame.size.width * i ) + 320   , 0, scrollView.frame.size.width, scrollView.frame.size.height);
    [self.scrollView addSubview:imageView];
    [imageView release];
}
// add the first image to last
imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:0]]];
imageView.frame = CGRectMake(scrollView.frame.size.width * ([imageArray count]+1), 0, scrollView.frame.size.width, scrollView.frame.size.height);
[self.scrollView addSubview:imageView];
[imageView release]; 

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([imageArray count]+ 2), self.scrollView.frame.size.height);
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(scrollView.frame.size.width,0,scrollView.frame.size.width,scrollView.frame.size.height) animated:NO]; 
}

- (IBAction)changePage :(id)sender
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage ;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;             
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//pageControlUsed = NO;

NSLog(@"%f", self.scrollView.contentOffset.x);

CGFloat pageWidth = self.scrollView.frame.size.width;
//pageNumber = floor((self.scrollView.contentOffset.x - pageWidth / ([imageArray count]+2)) / pageWidth) + 1  ;
pageNumber = self.scrollView.contentOffset.x / pageWidth;

if(pageNumber ==  0)
{
    [self.scrollView scrollRectToVisible:CGRectMake((self.scrollView.frame.size.width * [imageArray count]), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO];
    pageNumber = [imageArray count];
    //self.pageControl.currentPage = pageNumber;
}
else if(pageNumber == ([imageArray count]+1))
{
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO];
    pageNumber = 1;
    //self.pageControl.currentPage = pageNumber;
}

self.pageControl.currentPage = pageNumber - 1;

  }

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  {

  }

This Code works fine. Try this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文