UIScrollView不滚动,或者就此而言是交互式的

时间:2013-02-17 21:36:36

标签: iphone ios uiscrollview

我有以下层次结构:

VC - > UIView - >的UIScrollView

UIView恰好是UIScrollView的委托,并拥有UIScrollView作为实例变量。

UIView .h:

#import <UIKit/UIKit.h>
#import "ImageScrollView.h"


@interface Scroller : UIView <UIScrollViewDelegate>{

      ImageScrollView *scroller;
}


@end

的.m:

#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

的委托,则此代码可以正常工作

1 个答案:

答案 0 :(得分:0)

如果我实现您的代码,它可以正常工作。

为了更仔细地检查发生了什么,我添加了这一行:

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

在每个方法的开头。

如果我在scrollView中 pan ,则这是日志:

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:

您的触摸方法不会被调用,因为scrollView的内置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的触摸事件。