iOS UIScrollView的touchesBegan和touchesEnd

{app.params.name}} {app.params.name}} {app.params.name}}

使用UIScrollView的时候,总是调用不了touchesBegan和touchesEnd,还有另外的两个方法,经过查找是因为UIScrollViewDelegate没有这个方法,不过我们可以自己来定义这个方法,来调用父类的方法:

touchScrollView.h


//
//  touchScrollView.h
//  xunYi7
//
//  Created by david on 13-5-28.
//  Copyright (c) 2013年 david. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface touchScrollView : UIScrollView

@end
touchScrollView.m



//
//  touchScrollView.m
//  xunYi7
//
//  Created by david on 13-5-28.
//  Copyright (c) 2013年 david. All rights reserved.
//

#import "touchScrollView.h"

@implementation touchScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if ( !self.dragging )
    {
        [[self nextResponder] touchesBegan:touches withEvent:event];
    }
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    if ( !self.dragging )
    {
        [[self nextResponder] touchesEnded:touches withEvent:event];
    }
}

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    if(!self.dragging)
    {
        [[self nextResponder] touchesCancelled:touches withEvent:event];
    }
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    if(!self.dragging)
    {
        [[self nextResponder] touchesMoved:touches withEvent:event];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

使用方法可以这样

@property (strong, nonatomic) IBOutlet touchScrollView *scrollView;


版权声明

davidzhang 创作并维护的 Gowhich 博客采用 创作共用保留署名-非商业-禁止演绎4.0国际许可证。

本文首发于 Gowhich 博客( https://www.gowhich.com ),版权所有,侵权必究。

本文永久链接: https://www.gowhich.com/blog/161

comments powered by Disqus