gpt4 book ai didi

ios - 从另一个类调用时如何修复处理 PanGestureRecognizer 的错误?

转载 作者:行者123 更新时间:2023-12-01 16:30:14 26 4
gpt4 key购买 nike

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

#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 我这样称呼它:
- (void)viewWillAppear:(BOOL)animated {
CustomGestureRecognizer *cgr = [[CustomGestureRecognizer alloc] init];
[cgr initialize:[self view]];
}

如何解决这个问题?感谢您的回答。

最佳答案

因为cgr当你调用它时被释放。

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

- (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:)];

关于ios - 从另一个类调用时如何修复处理 PanGestureRecognizer 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com