从另一个类调用时,如何解决在处理PanGestureRecognizer时出现的错误? [英] How to fix error on handling PanGestureRecognizer when called from another class?

查看:38
本文介绍了从另一个类调用时,如何解决在处理PanGestureRecognizer时出现的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将PanGestureRecognizer添加到UIViewController的UIView中.我将视图划分为9个相等的部分,并由它们创建UIViews.另外,我在每个部分上都添加了PanGestureRocgnizer.当我在需要它的另一个类中调用它时,它可以正常工作,但是当我在屏幕上的某处触摸以启动PangestureRecognizer的句柄方法时,出现错误: [UIView handlePanGestureRocognizer:]:无法识别的选择器发送到实例0x17de2ee0.这是我的代码:

I want to add PanGestureRecognizer to UIView of UIViewController. I'm dividing view on nine equal parts and create an UIViews by them. Also I added PanGestureRocgnizer on every part. When I call it in another class where I need it it works fine but when I touch on the screen in some part to start handle method for PangestureRecognizer I get the Error: [UIView handlePanGestureRocognizer:]: unrecognized selector sent to instance 0x17de2ee0. This is my code:

#import "CustomGestureRecognizer.h"

@implementation CustomGestureRecognizer
@synthesize arrayOfPatternsForComparison;
@synthesize arrayOfSubviews;
@synthesize arrayOfPanGestureRecognizers;



- (void)initialize:(UIView *)view {
    arrayOfPatternsForComparison = [[NSMutableArray alloc] init];
    arrayOfSubviews = [[NSMutableArray alloc] init];
    arrayOfPanGestureRecognizers = [[NSMutableArray alloc] init];

    [self createPatternsForComparison];
    [self splitScreenInParts:view];
    [self setPanGestrueRecognizersOnEveryPartOfTheScreen];
}

- (void)createPatternsForComparison {

}

- (void)splitScreenInParts:(UIView *)view {
    CGSize onePart = CGSizeMake(view.frame.size.width/3, view.frame.size.height/3);
    CGFloat x_positionOfPart = 0;
    CGFloat y_positionOfPart = 0;

    for (NSUInteger j=0; j<3; j++) {
        for (NSUInteger i=0; i<3; i++) {
            UIView *_view = [[UIView alloc] initWithFrame:CGRectMake(x_positionOfPart, y_positionOfPart, onePart.width, onePart.height)];

            [[_view layer] setBorderWidth:1.0];
            [[_view layer] setBorderColor:[UIColor redColor].CGColor];

            x_positionOfPart += onePart.width;
            [arrayOfSubviews addObject:_view];
            [view addSubview:_view];
        }
        y_positionOfPart += onePart.height;
        x_positionOfPart = 0;
    }

}

- (void)setPanGestrueRecognizersOnEveryPartOfTheScreen {
    for (UIView *view in arrayOfSubviews) {
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];
        [[panGestureRecognizer view] setTag:[arrayOfSubviews indexOfObject:view]];
        [view addGestureRecognizer:panGestureRecognizer];
    }
}


- (void)handlePanGestureRocognizer:(UIPanGestureRecognizer *)sender {
    NSLog(@"%d", [[sender view] tag]);
}



@end

在另一类UIViewController中,我这样称呼它:

and in another class of UIViewController I called it like this:

- (void)viewWillAppear:(BOOL)animated {
    CustomGestureRecognizer *cgr = [[CustomGestureRecognizer alloc] init];
    [cgr initialize:[self view]];
}

如何解决此问题?感谢您的回答.

How to fix this? Thanks for your answers.

推荐答案

因为 cgr 在调用时已释放.

您必须在.h中创建var CustomGestureRecognizer * cgr ,然后:

You have to create a var CustomGestureRecognizer *cgr in .h and then:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    cgr = [[CustomGestureRecognizer alloc] init];
    [cgr initialize:[self view]];
}

然后更改此行

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestureRocognizer:)];

这篇关于从另一个类调用时,如何解决在处理PanGestureRecognizer时出现的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆