0

我有以下层次结构:

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 的委托,则此代码有效

4

1 回答 1

0

如果我实现你的代码,它工作正常。

为了更仔细地检查正在发生的事情,我添加了这一行:

NSLog (@"%@",NSStringFromSelector(_cmd));

在每个方法的开始。

如果我在 scrollView 内平移,这是日志:

ScrollViewTest1[4155:707] scrollViewDidScroll:
ScrollViewTest1[4155:707] 0
ScrollViewTest1[4155:707] scrollViewDidScroll:
ScrollViewTest1[4155:707] 0
ScrollViewTest1[4155:707] scrollViewDidScroll:
ScrollViewTest1[4155:707] 0
ScrollViewTest1[4155:707] scrollViewDidEndDecelerating:

由于滚动视图的内置 panGestureRecognizer 拦截了触摸,因此不会调用您的触摸方法。

如果我在scrollView内点击,这是日志:

ScrollViewTest1[4155:707] touchesBegan:withEvent:
ScrollViewTest1[4155:707] processTouch:
ScrollViewTest1[4155:707] scrollViewDidEndDecelerating:
ScrollViewTest1[4155:707] touchesEnded:withEvent:
ScrollViewTest1[4155:707] processTouch:

称为scrollView 的触摸不会为它自己的gestureRecognizer 抓取触摸事件。

于 2013-02-18T00:03:04.713 回答