I have a subclass of UIView that I want to be able to move around inside of it. When a user touches a UIView somewhere outside self.center, but inside self.boundsit βskipsβ because I add a new location in self.centerto achieve the actual move. To avoid this behavior, I am trying to set a reference point that allows the user to capture and drag the view anywhere in its borders.
My problem is that when I calculate a new anchor point (as shown in the code below), nothing happens, the view does not change position at all. On the other hand, if I set the anchor point to a pre-calculated point, I can move the view (but then, of course, it βjumpsβ to the pre-calculated point). Why is this not working properly?
Thank.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint locationInView = [touch locationInView:self];
CGPoint locationInSuperview = [touch locationInView:self.superview];
self.layer.anchorPoint = CGPointMake(locationInView.x / self.bounds.size.width, locationInView.y / self.bounds.size.height);
self.layer.anchorPoint = CGPointMake(0.01, 0.0181818);
self.center = locationInSuperview;
}
Oskar source
share