while touch dragging a view, i'm trying to anchor the view's center to the center of the screen and resize the view's width and height based on the drag. so if you touch the right of the square and drag towards the right, the left side of the square should become larger as well as the right side.
unfortunately, when dragging it's flashes from the center of screen to the mainScreen origin and i can't figure out why it's doing so. i think it has to do with the resetting of the origin settings because if i set the origin of the view to a static location in the (void)touchesMoved method, it works fine, but since the drag resizes the view, it also (is suppose to) changes it's origin. please help.
unfortunately, when dragging it's flashes from the center of screen to the mainScreen origin and i can't figure out why it's doing so. i think it has to do with the resetting of the origin settings because if i set the origin of the view to a static location in the (void)touchesMoved method, it works fine, but since the drag resizes the view, it also (is suppose to) changes it's origin. please help.
Code:
- (void)viewDidLoad
{
CGRect fullScreenRect = [[UIScreen mainScreen] bounds];
UIView *aView = [[UIView alloc] initWithFrame:fullScreenRect];
aView.backgroundColor = [UIColor redColor];
self.background = aView;
[aView release];
CGRect aFrame = CGRectMake(0, 0, 50, 50);
aFrame.origin = CGPointMake(CGRectGetMidX(fullScreenRect) -
CGRectGetMidX(aFrame), CGRectGetMidY(fullScreenRect) -
CGRectGetMidY(aFrame));
UIView *aBoxView = [[UIView alloc] initWithFrame:aFrame];
aBoxView.backgroundColor = [UIColor whiteColor];
self.boxView = aBoxView;
[aBoxView release];
[self.view insertSubview:background atIndex:0];
[self.view insertSubview:boxView atIndex:1];
[super viewDidLoad];
}
- (void)dealloc
{
[background release];
[boxView release];
[super dealloc];
}
#pragma mark Touch Methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self.view];
firstTouchPoint = point;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect fullScreenRect = [[UIScreen mainScreen] bounds];
CGPoint point = [[touches anyObject] locationInView:self.view];
CGRect boxFrame = boxView.frame;
boxFrame.size.width += point.x - firstTouchPoint.x;
boxFrame.size.height += point.y - firstTouchPoint.y;
boxFrame.origin = CGPointMake(CGRectGetMidX(fullScreenRect) -
CGRectGetMidX(boxFrame), CGRectGetMidY(fullScreenRect) -
CGRectGetMidY(boxFrame));
[boxView setFrame:boxFrame];
firstTouchPoint = point;
NSString *log = [NSString stringWithFormat:@"%f, %f",
boxFrame.origin.x, boxFrame.origin.y];
NSLog(log);
}