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