Troubles moving a UIView.
- by Joshua
I have been trying to move a UIView by following a users touch. I have almost got it to work except for one thing, the UIView keeps flicking between two places.
Here's the code I have been using:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchDown");
UITouch *touch = [touches anyObject];
firstTouch = [touch locationInView:self.view];
lastTouch = [touch locationInView:self.view];
[self.view setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
InSightViewController *contentView = [[InSightViewController alloc] initWithNibName:@"SubView" bundle:[NSBundle mainBundle]];
[contentView loadView];
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];
if (CGRectContainsPoint(contentView.view.bounds, firstTouch)) {
NSLog(@"touch in subView/contentView");
sub.frame = CGRectMake(currentTouch.x - 50.0, currentTouch.y, 130.0, 21.0);
}
NSLog(@"touch moved");
lastTouch = currentTouch;
[self.view setNeedsDisplay];
}
And here's what's been happening: http://cl.ly/Sjx