1

我有一个简单UIViewController的 9 UIImageViews。当每一个UIImageView被按下时,一个方法(或函数)被调用。这一切都很好,但问题是我现在有太多的样板代码。

在我的viewDidLoad方法中,我有 9 个UITapGestureRecognizer来检测我的 9 个中的任何一个UIImageViews被按下的时间。然后他们调用一个方法来运行。这是我的代码:

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed1:)];
[picview_1 addGestureRecognizer:tap1];

UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed2:)];
[picview_2 addGestureRecognizer:tap2];

UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed3:)];
[picview_3 addGestureRecognizer:tap3];

UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed4:)];
[picview_4 addGestureRecognizer:tap4];

UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed5:)];
[picview_5 addGestureRecognizer:tap5];

UITapGestureRecognizer *tap6 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed6:)];
[picview_6 addGestureRecognizer:tap6];

UITapGestureRecognizer *tap7 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed7:)];
[picview_7 addGestureRecognizer:tap7];

UITapGestureRecognizer *tap8 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed8:)];
[picview_8 addGestureRecognizer:tap8];

UITapGestureRecognizer *tap9 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed9:)];
[picview_9 addGestureRecognizer:tap9];

以下是被调用的方法:

-(void)imagepressed1:(UIGestureRecognizer*)sender {
ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
self.seconddata = screen;
seconddata.page_num = page;
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed2:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 1;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed3:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 2;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed4:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 3;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed5:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 4;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed6:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 5;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed7:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 6;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed8:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 7;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

-(void)imagepressed9:(UIGestureRecognizer*)sender {
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + 8;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}

如您所见,我的方法几乎都相同,除了一个小细节,称为“页面”的整数根据功能增加了不同的数字。

有什么方法可以实现与上述相同的功能,但没有太多不专业的代码副本?

谢谢,丹。

4

3 回答 3

2

Put picview_N in an array, and add a different recognizer to them in a loop. Give each picview_N a tag that corresponds to the number that you want added to the page_num, and use sender.view.tag to find that number at runtime:

NSArray *picViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
NSUInteger tag = 1;
for (UIView *picView in picViews) {
    picView.tag = tag++;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
    [picView addGestureRecognizer:tap];
}
...
-(void)imagepressed:(UIGestureRecognizer*)sender { // Common for all recognizers
    ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
    self.seconddata = screen;
    seconddata.page_num = page + sender.view.tag;
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:screen animated:YES completion:nil];
}
于 2013-07-31T13:37:18.337 回答
0

你想用的是

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

此方法返回包含指定点的视图层次结构(包括其自身)中接收器的最远后代。

因此,假设您在对象UITapGapstureRecogniser上设置了一组,UIView并且为每个 imageview 设置了标签属性:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewPressed:)];
[_view_1 addGestureRecognizer:tap];


- (IBAction)viewPressed:(UIGestureRecogniser *)sender{
   CGPoint touchPoint = [sender locationInView:self.view];
   UIView *touchView = [self.view hitTest:touchPoint withEvent:nil];
   if(touhView isKindOfClass:[UIImageView class] && touchView.tag == 1)
   {
     // Do action on touch of imageview 1
   } 
   else if(touhView isKindOfClass:[UIImageView class] && touchView.tag == 2) {
    // Do action on touch of imageview 2
   }
   // similarly for other image views
}
于 2013-07-31T13:36:29.917 回答
0

这是一种解决方案,但它仍然不是很好......

picview_1.tag = 0;
picview_2.tag = 1;
picview_3.tag = 2;
picview_4.tag = 3;
picview_5.tag = 4;
picview_6.tag = 5;
picview_7.tag = 6;
picview_8.tag = 7;
picview_9.tag = 8;

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_1 addGestureRecognizer:tap1];

UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_2 addGestureRecognizer:tap2];

UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_3 addGestureRecognizer:tap3];

UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_4 addGestureRecognizer:tap4];

UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_5 addGestureRecognizer:tap5];

UITapGestureRecognizer *tap6 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_6 addGestureRecognizer:tap6];

UITapGestureRecognizer *tap7 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_7 addGestureRecognizer:tap7];

UITapGestureRecognizer *tap8 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_8 addGestureRecognizer:tap8];

UITapGestureRecognizer *tap9 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_9 addGestureRecognizer:tap9];

至于方法:

-(void)imagepressed:(UIGestureRecognizer*)sender {
ImageViewer *screen = [[ImageViewer alloc] initWithNibName:nil bundle:nil];
self.seconddata = screen;
seconddata.page_num = page + sender.view.tag;
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:screen animated:YES completion:nil];

}

如果有人知道我可以如何改进这一点,那将非常感谢。

于 2013-07-31T14:18:10.413 回答