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

Posted

技术标签:

【中文标题】滚动视图滚动时页面控制不改变【英文标题】:Page Control not changing when scroll view scrolls 【发布时间】:2011-11-21 09:59:00 【问题描述】:

我正在尝试将页面控件添加到我的滚动视图中,并且遵循了许多网络教程,其中大多数使用与 tutorial. 相同的代码但是,一旦我将代码放入我的项目中,即使我正在制作更改代码以尝试使其正常工作,但事实并非如此。我已经设法使代码在按下页面控件时工作,但是它不适用于页面滚动。我的问题类似于this,尽管答案没有帮助。这是我的代码:

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 中链接。

【问题讨论】:

【参考方案1】:

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

因为它没有定义委托,所以滚动视图不会调用你的 scrollViewDidScroll 函数。

提姆

【讨论】:

非常感谢!每次我检查代码时,那条线都会从我身边飞过。那一行和下面一行就完成了: svCollegeMain.delegate = self; (.m) 和 .h 中的 。谢谢【参考方案2】:

试试这个

头文件:

@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
  

  

此代码运行良好。试试这个

【讨论】:

以上是关于滚动视图滚动时页面控制不改变的主要内容,如果未能解决你的问题,请参考以下文章

将一个视图滑过另一个视图

android应用模仿android桌面效果,就是拖动页面时背景图片随之滚动,但是滚动的幅度比页面拖动的幅度小。

在单元格上交互时集合视图不滚动

iOS页面控制滚动锁定

页面应用程序 + 自动布局 + 子视图在滚动时缩放

在教程屏幕上使用滚动视图和页面控制器