TouchesBegan with bringSubviewToFront

时间:2017-06-22 14:05:03

标签: ios objective-c uigesturerecognizer touch-event bringtofront

我正在为Gestrue Recognizer工作,在视图中有许多UIView用于旋转,捏合和平移动作。 我的问题是当我触摸时,如何使我的UIView之一带上SubviewToFront,例如,like my photo,当我触摸它时,我希望绿色的一个带到前面。 (所以蓝色变为绿色背后)

每个UIView都来自我的NoteView类

NoteView * noteView = [[NoteView alloc]initWithFrame:CGRectMake(50, 50, 150, 150)];
[self setGesture:noteView];
[self.view addSubview:noteView];

TouchsBegan我正在这样工作

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.note = [NoteView new];

UITouch * touch = [touches anyObject];
if (touch.view == self.note) {
    [self.view bringSubviewToFront:self.note];
}}

1 个答案:

答案 0 :(得分:0)

感谢您的帮助!!我解决了我的问题,也许有人也会有同样的问题,所以我更新了我的答案,我使用user3589771的link来弄明白。

如下面的代码

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  CGPoint location = [[touches anyObject] locationInView:self.view];
  CGRect fingerRect = CGRectMake(location.x-5, location.y-5, 10, 10);

  for(UIView *view in self.view.subviews){
    CGRect subviewFrame = view.frame;

    if(CGRectIntersectsRect(fingerRect, subviewFrame)){
        NSLog(@"Yeah !, i found it %@",view);
        [self.view bringSubviewToFront:view];
    }
}}

但实际上我并没有放弃对

的理解
for(UIView *view in self.view.subviews){
    CGRect subviewFrame = view.frame;

    if(CGRectIntersectsRect(fingerRect, subviewFrame)){

    }}

顺便说一句,如果有人可以就这个新问题给我一个评论,那将会很棒。