addSubview重置anchorPoint

时间:2015-01-15 21:08:29

标签: ios objective-c uiview ios8 uigesturerecognizer

我正在使用苹果示例方法执行平移和捏合,以保持手指捏的缩放位置。代码移动手指触摸的图层锚点。一切正常,但是当我添加子视图时,锚点重置并且一切都不合适。 我试图更改子视图锚点以匹配超级视图,但它返回到默认锚点。

捏合和锚点调整的代码示例:

/**
Scale and rotation transforms are applied relative to the layer's anchor point
this method moves a gesture recognizer's view's anchor point between the user's 
fingers.
*/
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}

/**
Scale the piece by the current scale.
Reset the gesture recognizer's rotation to 0 after applying so the next callback 
is a delta from the current scale.
*/
- (IBAction)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale],[gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
    }
}

我有一个self.canvasView和一个self.workingView手势在self.canvasView中,self.workingView是self.canvasView的子视图。

一切正常,每次我向self.workingView添加一个子视图时,锚点重置并且一切都不合适。

我该如何避免这种行为?我无法在文档中发现addSubview重置了anchorPoint,因此我假设我在这里做错了。

0 个答案:

没有答案