1

I am working for Gestrue Recognizer with many UIView for rotation, pinch and pan action in a view. My question is how to make one of my UIView bringSubviewToFront when I touched, for example, like my photo, I want green one bring to front when I touch it. (so blue change to behind of green)

each UIView are init from my NoteView class

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

TouchsBegan I am working like this

-(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];
}}
Zoe
  • 121
  • 1
  • 2
  • 8
  • `if (CGRectIntersectsRect(someView.frame, otherView.frame){[self.view bringSubViewToFront:oneOfTheView];}`? – Larme Jun 22 '17 at 14:13
  • in your touchesBegan:withEvent: method, the first thing you do is create a new NoteView and assign it to a note property. if (touch.view == self.note) can then never be true (the touch can't contain the new note view because you have just created it), so the code within the scope of the if statement will never be executed. – TheBasicMind Jun 22 '17 at 14:40
  • You pointing out my mistakes !! Could you tell me how can I get the current view I touched? thanks! – Zoe Jun 22 '17 at 14:48
  • [Check this out](https://stackoverflow.com/a/16329436/3589771) – Mathi Arasan Jun 22 '17 at 15:12

1 Answers1

0

Thanks for helping !! I solve my question, maybe someone also will have same question, so I update my answer, I use user3589771's link to figure out it.

Like below's code

-(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];
    }
}}

but actually I'm not quit understanding about

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

    if(CGRectIntersectsRect(fingerRect, subviewFrame)){

    }}

By the way, if someone can give me a comment for this new question that will be great.

Zoe
  • 121
  • 1
  • 2
  • 8