如何修复从另一个 class 调用时处理 PanGestureRecognizer 的错误?
How to fix error on handling PanGestureRecognizer when called from another class?
我想将 PanGestureRecognizer 添加到 UIViewController 的 UIView。我将视图分成九个相等的部分,并按它们创建一个 UIViews。我还在每个部分添加了 PanGestureRocgnizer。当我在另一个 class 需要它的地方调用它时,它工作正常但是当我触摸屏幕的某些部分以启动 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
在另一个 class 的 UIViewController 中,我这样称呼它:
- (void)viewWillAppear:(BOOL)animated {
CustomGestureRecognizer *cgr = [[CustomGestureRecognizer alloc] init];
[cgr initialize:[self view]];
}
如何解决这个问题?感谢您的回答。
因为cgr
调用的时候就释放了
您必须在 .h 中创建一个变量 CustomGestureRecognizer *cgr
然后:
- (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:)];
这一行有问题。
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];
在这里您将目标作为 "view" 传递,这是一个子视图。目标应该是 viewController 对象,例如 "self" 或任何其他 viewController 对象。
而且在这里您只是创建了 9 个视图(我认为是在 NSObject 子类中)并添加到一个数组中,但是这些视图没有作为子视图添加到任何视图中。为此,您可以 return 将该数组添加到您的 viewController,然后向那些以自身为目标的视图添加平移手势。然后它会工作。
我通过在 .h[=21 中添加 var *CustomGestureRecognizer cgr 解决了这个问题=] 另一个 class 就像 anhtu 说的那样并更改
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];
目标 self 就像 Dev 说的那样。
我想将 PanGestureRecognizer 添加到 UIViewController 的 UIView。我将视图分成九个相等的部分,并按它们创建一个 UIViews。我还在每个部分添加了 PanGestureRocgnizer。当我在另一个 class 需要它的地方调用它时,它工作正常但是当我触摸屏幕的某些部分以启动 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
在另一个 class 的 UIViewController 中,我这样称呼它:
- (void)viewWillAppear:(BOOL)animated {
CustomGestureRecognizer *cgr = [[CustomGestureRecognizer alloc] init];
[cgr initialize:[self view]];
}
如何解决这个问题?感谢您的回答。
因为cgr
调用的时候就释放了
您必须在 .h 中创建一个变量 CustomGestureRecognizer *cgr
然后:
- (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:)];
这一行有问题。
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];
在这里您将目标作为 "view" 传递,这是一个子视图。目标应该是 viewController 对象,例如 "self" 或任何其他 viewController 对象。
而且在这里您只是创建了 9 个视图(我认为是在 NSObject 子类中)并添加到一个数组中,但是这些视图没有作为子视图添加到任何视图中。为此,您可以 return 将该数组添加到您的 viewController,然后向那些以自身为目标的视图添加平移手势。然后它会工作。
我通过在 .h[=21 中添加 var *CustomGestureRecognizer cgr 解决了这个问题=] 另一个 class 就像 anhtu 说的那样并更改
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:view action:@selector(handlePanGestureRocognizer:)];
目标 self 就像 Dev 说的那样。