UIPinchGestureRecognizer position the pinched view between the two fingers

前端 未结 2 995
时光取名叫无心
时光取名叫无心 2020-12-02 07:34

I successfully implemented a pinch a zoom of a view. However, the view doesn\'t position itself where I wished it to be. For the stackoverflowers with an iPad, I would like

2条回答
  •  难免孤独
    2020-12-02 07:58

    Have a look at the Touches sample project. Specifically these methods could help you:

    // 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
    - (void)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];
        }
    }
    

提交回复
热议问题