点击一个物体后将其放在其他物体的前面

Put an object in front of every others after a tap on it

所以,我有一个个性化对象 (PostIt.xib)。 我有一个功能:addNewPostIt,当我按下 good 按钮时...是的,你明白了(提示:它添加了一个新的 postit ;))。

所以我可以通过按下按钮来创建很多,而且每个帖子都是可拖动的(感谢平移手势识别器)。

现在,我想要做的是,当我点击其中一个时,它会把它放在所有其他人的前面。

我想我显然应该使用点击手势识别器,但我不知道如何处理... 有谁可以帮助我吗?非常感谢!

编辑:我有一个 UITapGestureRecognizer,链接在我的 PostIt.h 下:

这里是我的addNewPostIt函数,如果你想看的话:

- (IBAction)addNewPostIt:(id)sender {
    PostIt *postit = [[[NSBundle mainBundle] loadNibNamed:@"PostIt" owner:self options:nil] firstObject];
    postit.frame = CGRectMake(x, y, 400, 400);
    [self.view addSubview:postit];
    x+=50;
    y+=50;
}

-(IBAction)tap:(UITapGestureRecognizer *)recognizer

如果您希望该视图位于所有视图之上,则必须使用此

[self.view bringSubviewToFront:self.postit.view];

也看到这个 link 添加子视图

好的,试试这个,

- (IBAction)addNewPostIt:(id)sender {
   PostIt *postit = [[[NSBundle mainBundle] loadNibNamed:@"PostIt" owner:self options:nil] firstObject];
  postit.frame = CGRectMake(x, y, 400, 400);
  [self addTapGestureTo:postit];
  [self.view addSubview:postit];
  x+=50;
  y+=50;

}

- (void)addTapGestureTo:(PostIt *)postit
{
   UITapGestureRecognizer *aTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedPostIt:)];
   [postit addGestureRecognizer:aTapGesture];
}

- (void)tappedPostIt:(UIGestureRecognizer *)tapGesture
{
  UIView *targetView = tapGesture.view;
  [self.view bringSubviewToFront:targetView];
}

如果你想要 PostIt.h 文件中的 tap: 方法,那么你可以像下面那样做

PostIt.m 文件中,您需要像下面这样处理点击, 超级视图包含您添加的所有 PostIt 视图,因此您必须调用超级视图的 bringSubviewToFront 方法

- (IBAction)tap:(id)sender {
  [self.superview bringSubviewToFront:self];
}