As Kris Van Bael pointed out, your going to need to do the anchor point calculations in the touchsBegan:withEvent:
method in order not to negate the movement. Additionally, since changing the layer's anchorPoint
will move the view's initial position, you have to add an offset to the view's center
point to avoid a 'jump' after the first touch.
You can do this by calculating (and adding to your view's center
point) the offset based on the difference between the initial and final anchorPoints (multiplied by your view's width/height) or you could set the view's center
to the initial touch point.
Something like this perhaps:
- (void)touchesBegan:(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.frame.size.width, locationInView.y / self.frame.size.height);
self.center = locationInSuperview;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint locationInSuperview = [touch locationInView:self.superview];
self.center = locationInSuperview;
}
More info on anchorPoint
's from apple's docs here and a similar SO question I referenced here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…