首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >锅炉板规范太多.方法

锅炉板规范太多.方法
EN

Stack Overflow用户
提问于 2013-07-31 13:29:19
回答 3查看 223关注 0票数 1

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

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

代码语言:javascript
复制
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];

下面是正在被调用的方法:

代码语言:javascript
复制
-(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“的Integer将根据函数的不同而增加一个不同的数字。

有任何方法,我可以实现上述相同的功能,但没有那么多的非专业的副本,我的代码?

谢谢丹。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-31 13:37:18

picview_N放在数组中,并在循环中向它们添加不同的识别器。为每个picview_N提供一个与您希望添加到page_num中的数字相对应的标记,并使用sender.view.tag在运行时查找该数字:

代码语言:javascript
复制
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];
}
票数 2
EN

Stack Overflow用户

发布于 2013-07-31 13:36:29

你想用的是

代码语言:javascript
复制
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

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

因此,假设您在您的UITapGapstureRecogniser对象上设置了一个UIView,并且为每个图像视图设置了标记属性:

代码语言:javascript
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2013-07-31 14:18:10

这是一种解决办法,但它仍然不是很好.

代码语言:javascript
复制
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];

至于方法:

代码语言:javascript
复制
-(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];

}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17971782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档