Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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 ?
 
Last edited:
You might want to revisit this previous thread of yours. It shows a way.
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.
 
Last edited:
I don't understand what you're trying to say here.

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.
 
First of: I'm not even sure what your code is supposed to be doing. Could you provide an explanation of its purpose?

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

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?

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

I can't try it since I don't have enough code. Besides, I'm not prepared to do your debugging for you.
 
I don't understand what you're trying to say here.

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?

Now I removed some useless int vars like.. curX = touchPoint.x
I see it only happens with the 1st square thats being draw. I get 2 squares and it looks like they are relative to eachother ! :-/



I can't try it since I don't have enough code. Besides, I'm not prepared to do your debugging for you.

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.
 
I understand, but this IS all the code, except for...

Well, I could assume about myView and self.view, but those were at least a couple more undeclared variables, given the code you supplied.

But the issue was because the default 'currentFrame' was 0.

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?
 
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?

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
 
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, 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.