我有以下层次结构:
VC -> UIView -> UISCrollView
UIView 恰好是 UIScrollView 的委托,并且拥有 UIScrollView 作为实例变量。
界面视图.h:
#import <UIKit/UIKit.h>
#import "ImageScrollView.h"
@interface Scroller : UIView <UIScrollViewDelegate>{
ImageScrollView *scroller;
}
@end
米:
#import "Scroller.h"
@implementation Scroller
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
DLog(@"");
[self initScroll];
}
return self;
}
-(void)initScroll{
DLog(@"");
scroller = [[ImageScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200, 200)];
scroller.delegate = self;
[self addSubview:scroller];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.frame.size.width;
int page = floor((scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
DLog(@"%d",page);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
DLog(@"");
}
注意:到目前为止,没有一个委托方法被调用。但是,我将 iphone 屏幕上的 UIScrollView 视为黑框。
#import "ImageScrollView.h"
@implementation ImageScrollView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
DLog(@"%f %f",frame.size.width,frame.size.height);
[self configUI];
[self configSelf];
}
return self;
}
-(void)configUI{
self.backgroundColor = [UIColor blackColor];
self.contentSize = CGSizeMake(1000, 400);
self.showsHorizontalScrollIndicator = YES;
}
-(void)configSelf{
self.scrollEnabled = YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
DLog(@"");
for (UITouch *touch in touches) {
CGPoint _location = [touch locationInView:touch.view];
[self processTouch:_location];
}
[self.delegate scrollViewDidEndDecelerating:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
DLog(@"");
for (UITouch *touch in touches) {
CGPoint _location = [touch locationInView:touch.view];
[self processTouch:_location];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
DLog(@"");
for (UITouch *touch in touches) {
CGPoint _location = [touch locationInView:touch.view];
[self processTouch:_location];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
注意:在 IUScrollViewSubclass 中都不会调用 touchesEnded 或 touchesMoved 方法。
我究竟做错了什么?
但是,如果我将视图控制器分配为 uiscrollview 的委托,则此代码有效