捕获ScrollView中的单/双击的问题

时间:2011-07-01 16:08:07

标签: iphone objective-c uiscrollview

我发布此消息是因为我一直在阅读论坛,但我找不到类似的问题。我需要能够区分点击和双击(这是一个标准的事情)但我的问题是,无论出于何种原因,我在另一个ScrollView内部有一个Scroll View。所以,我不得不将我的ScrollView子类化,以便获得被调用的touBegin方法。

我有一个名为PhotoViewController的类(BaseViewController的子类),这个类包含另一个名为CustomScrollView的类(ScrollView的子类)。我需要从ScrollView对这个CustomScrollView进行子类化,以便覆盖touchesBegin方法,并能够捕获用户所做的触摸。

我尝试使用touchesBegin方法中的return [super touchesBegan:touches withEvent:event]之类的东西调用CustomScrollView中的touchesBegin方法,但是当调用PhotoViewController中的touchesBegin方法时,它的参数是空的(我无法辨别是否用户单击或双击,这正是我所需要的)

我有一个名为PhotoViewController的课程:

@class PhotoViewController
@interface PhotoViewController : BaseViewController  <UIScrollViewDelegate> {

    CustomScrollView*           myScrollView;

}

@implementation PhotoViewController

...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    NSUInteger tapCount = [touch tapCount];

    switch (tapCount) {
        case 1:

            [self performSelector:@selector(singleTapMethod) withObject:nil afterDelay:.4];
            break;
        case 2:

            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapMethod) object:nil];
            [self performSelector:@selector(doubleTapMethod) withObject:nil afterDelay:.4];
            break;
        default:
            break;
    }

}

类CustomScrollView是(CustomScrollView.h):

@interface CustomScrollViewPhoto : UIScrollView {
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

@end

它的实现就是这个(CustomScrollView.m):

@implementation CustomScrollViewPhoto

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    [self.superview touchesBegan:[NSSet set] withEvent:event];
    return [super touchesBegan:touches  withEvent:event];
}

我是否会按照我想要的方向走错路?也许,我应该捕获CustomScrollView类中的点击/双击(这很好用!),并从那里使用@selector或调用PhotoViewController中适当的方法?

感谢阅读!

1 个答案:

答案 0 :(得分:0)

我认为你的路线错误(只是略有一点!)。我在照片查看器中执行了类似的操作,并捕获了CustomScrollView中的触摸。你不应该在PhotoViewController中做任何事情。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    if(touch.tapCount == 2)
    {
        if (self.zoomScale == self.minimumZoomScale)
        {
            //Zoom where the user has clicked from
            CGPoint pos = [touch locationInView:self];
            [self zoomToRect:CGRectMake((pos.x - 5.0)/self.zoomScale, (pos.y-5.0)/self.zoomScale, 10.0, 10.0) animated:YES];
        }
        else
        {
            //Zoom back out to full size
            [self setZoomScale:self.minimumZoomScale animated:YES];
        }
    }
}