Resolved programaticly grab UIView

Discussion in 'iOS Programming' started by DennisBlah, Apr 20, 2015.

  1. DennisBlah, Apr 20, 2015
    Last edited: Apr 20, 2015

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I'm wondering if there is a quick and simple solution for the following.
    I know I can loop through subviews etc. So that's 1.

    But issn't it possible to select a subview based on it's tag ?

    I want to re-size a subview based on a tag.

    Is this possible? Like.. you do with an dictionary ?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. DennisBlah, Apr 20, 2015
    Last edited: Apr 20, 2015

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Hi dejo,

    thanks for your time reading the post. And my apologies I did not reply on the older thread thats almost simulair to this one. I'll put it on solved btw.
    But the viewWithTag is a bit buggy.

    Could you have a look at this:

    Code:
    #pragma mark - View lifecycle
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        lowestX = self.view.frame.size.width;
        lowestY = self.view.frame.size.height;
        highestX = 0;
        highestY = 0;
        width = 0;
        height = 0;
        
        if(touches.count == 2 || touches.count == 4) {
            // iterate through our touch elements
            for (UITouch *touch in touches) {
                // get the point of touch within the view
                CGPoint touchPoint = [touch locationInView: myView];
                
                if([touches count] == 2 || [touches count] == 4) {
                    int curX = touchPoint.x;
                    int curY = touchPoint.y;
                    if(curX < lowestX)
                        lowestX = curX;
                    
                    if(curY < lowestY)
                        lowestY = curY;
                    
                    curX = touchPoint.x;
                    curY = touchPoint.y;
                    if(curX > highestX)
                        highestX = curX;
                    
                    if(curY > highestY)
                        highestY = curY;
                }
            }
            width = highestX - lowestX;
            height = highestY - lowestY;
            
            CGRect newFrame = CGRectMake(lowestX, lowestY, width, height);
            UIView *newView = [[UIView alloc] initWithFrame: newFrame];
            [newView setBackgroundColor: [UIColor redColor]];
            [newView setTag: currentFrame];
            [self.view addSubview: newView];
            [myView bringSubviewToFront: newView];
            
            [self.view bringSubviewToFront: myView];
        }
    }
    
    // touches moved will update touch view positions
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        if(touches.count == 2 || touches.count == 4) {
            lowestX = self.view.frame.size.width;
            lowestY = self.view.frame.size.height;
            highestX = 0;
            highestY = 0;
            width = 0;
            height = 0;
            
            // iterate through our touch elements
            for (UITouch *touch in touches) {
                // get the point of touch within the view
                CGPoint touchPoint = [touch locationInView: myView];
                
                if([touches count] == 2 || [touches count] == 4) {
                    int curX = touchPoint.x;
                    int curY = touchPoint.y;
                    if(curX < lowestX)
                        lowestX = curX;
                    
                    if(curY < lowestY)
                        lowestY = curY;
                    
                    curX = touchPoint.x;
                    curY = touchPoint.y;
                    if(curX > highestX)
                        highestX = curX;
                    
                    if(curY > highestY)
                        highestY = curY;
                }
            }
            width = highestX - lowestX;
            height = highestY - lowestY;
            
            for (UIView *curSub in self.view.subviews) {
                if(curSub.tag == currentFrame) {
                    //    UIView *curSub = [self.view viewWithTag: currentFrame];
                    
                    CGRect newFrame = CGRectMake(lowestX, lowestY, width, height);
                    [curSub setFrame: newFrame];
                    [curSub setBackgroundColor: [UIColor blueColor]];
                    [myView bringSubviewToFront: curSub];
                    break;
                }
            }
    
        }
        [self.view bringSubviewToFront: myView];
    }
    
    // touches end needs to removed any added touch views
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        currentFrame++;
        [self.view bringSubviewToFront: myView];
    }
    
    // touches cancelled has same behavior as touches ended
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        [self touchesEnded: touches withEvent:event];
    }
    
    Also trying to always the 'myView' in the front, so my touches are based on the myView, and not any other.

    Somehow I can't draw new rectangles within a other.

    Getting rid of all the dictionaries now since their useless and only taking up space and performance.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I don't understand what you're trying to say here.
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    I made a view and assigned the property myView to it and making sure it's always in the foreground.
    So my touches will not get related to the views I'm 'drawing'.

    But yet with the [self.view viewWithTag: currentFrame]; it's really buggy.

    You should try it, I can't really explain it.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    First of: I'm not even sure what your code is supposed to be doing. Could you provide an explanation of its purpose?

    Really buggy how? What debugging have you done to investigate what's going wrong? Be as detailed as possible to explain it. What do you expect the code to do? What does it do instead?

    I can't try it since I don't have enough code. Besides, I'm not prepared to do your debugging for you.
     
  7. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #7
    I understand, but this IS all the code, except for
    lowestX
    lowestY
    highestX
    highestY
    width
    height

    These are all integers. and a property for a overlaying view for touching.

    But the issue was because the default 'currentFrame' was 0.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Well, I could assume about myView and self.view, but those were at least a couple more undeclared variables, given the code you supplied.

    Ah, good. Glad to hear you were able to debug it on your own. Just to clarify, the issue was because the default tag on currentFrame was 0, correct?
     
  9. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #9
    The self.view is already defined by default on your viewcontroller in storyboard.
    Indeed the myView is also an UIView. Sorry for being unclear

    Yes indeed the currentFrame was default 0. Because the all views have default tag 0 I think the code tries to move and resize everything in the self.view
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Yes, please try to be as specific as possible in the future. It really helps!

    Although the code snippet would indicate it was part of a UIViewController, that was not explicitly stated. The more you can do to avoid having those you seek help from making assumptions about your code the better. Thanks.
     

Share This Page