我有一个简单的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];
}
正如你所看到的,除了一个小细节外,我的方法几乎都是相同的,整数称为" page"正在根据功能增加不同的数字。
有没有什么方法可以实现与上面相同的功能但没有那么多非专业的代码副本?
谢谢,Dan。
答案 0 :(得分:2)
将picview_N
放入数组中,并在循环中为它们添加不同的识别器。为每个picview_N
添加与您要添加到page_num
的数字相对应的标记,并使用sender.view.tag
在运行时查找该数字:
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];
}
答案 1 :(得分:0)
您想要使用的是
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
此方法 返回包含指定点的视图层次结构(包括其自身)中接收器的最远后代。
因此,假设您在UITapGapstureRecogniser
对象上设置了一个UIView
,并为每个图像视图设置了标记属性:
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
}
答案 2 :(得分: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];
}
如果有人知道如何改善这一点,那将非常感谢。