对于我的 iPad 应用程序,我有一个主 ViewController,其中包含两个 UIScrollview 和一个 UIPageControl。问题是寻呼的代表没有被调用。这是布局:
在底部 thumbScrollView 中选择一个按钮需要更新 mainScrollView 中的图像(这可行) 滑动 thumbScrollView 或在 pageControl 上选择一个点需要“分页” thumbScrollView 以显示下一组按钮。刷卡不起作用,因为委托函数没有被调用。
我在我的 VC 中声明滚动视图和页面控件如下
@property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
@property (strong, nonatomic) IBOutlet UIScrollView *thumbScrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
ViewController 实现 UIScrollViewDelegate
@interface MyViewController : UIViewController<UIScrollViewDelegate>
我在我的 VC 的 .m 文件中实现了以下 UIScrollViewDelegate 委托函数。
- (void)scrollViewDidScroll:(UIScrollView *)sender;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
视图出现了,但是当我在按钮上滑动时,我没有看到上面的委托函数被调用。
我没有在 StackOverflow 中找到解决方案,尽管我已经考虑了其他相关帖子的其他方面的建议(例如,区分哪个滚动视图已启动操作的逻辑等)
在此处添加详细代码(根据@HeWas 的要求)
这是控制两个滚动视图和页面控件的主视图控制器的头文件(相关摘录 - 如果您需要更多信息,请告诉我)
// ImageBrowseViewController.h
// (NOTE - In Interface Builder I have added a tag attribute of 0 to mainScrollView
// and 1 to thumbScrollView, to enable me to distinguish which scrollView the delegate
// needs to respond to)
#define TAG_MAIN_SCROLLVIEW 0
#define TAG_THUMB_SCROLLVIEW 1
@interface ImageBrowseViewController : UIViewController<UIScrollViewDelegate>
{
UIButton* currentlySelectedButton;
UIScrollView *mainScrollView;
UIScrollView *thumbScrollView;
UIPageControl* pageControl;
BOOL pageControlBeingUsed;
}
@property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
// … connected as outlet in IB to mainScrollView
@property (strong, nonatomic) IBOutlet UIScrollView * thumbScrollView;
// … connected as outlet in IB to thumbScrollView
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
// … connected as outlet in IB to pageControl
…
-(IBAction)changePage; //Touch up Inside IBAction connected to pageControl
…
@end
这是控制两个滚动视图和页面控件的主视图控制器的实现文件(相关摘录 - 如果您需要更多信息,请告诉我)
//
// ImageBrowseViewController.m
//
…
@synthesize mainScrollView;
@synthesize thumbScrollView;
@synthesize pageControl;
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if ( [sender tag] == TAG_THUMB_SCROLLVIEW ) {
// This is the thumbScrollview
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.thumbScrollView.frame.size.width;
int page =
floor((self.thumbScrollView.contentOffset.x - pageWidth / 2) / pageWidth)
+ 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
//frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.x = self.thumbScrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.thumbScrollView.frame.size;
[self.thumbScrollView scrollRectToVisible:frame animated:YES];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}