1

私はAppleのScrollViewSuiteであるTapToZoomアプリをいじくり回して、ズームした画像にビューを配置しようとしています。これが私がやりたいことです:ダブルタップで、画像をpi / 2で回転し、8倍にズームしてから、変換された画像の上にあるscrollViewにサブビューを配置します。

元の座標系でのビューの目的のフレームがわかっています。したがって、ズーム後にこれらの座標を新しいシステムに変換する必要があります...

ScrollViewSuiteから...

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // double tap zooms in
    float newScale = [imageScrollView zoomScale] * 8;  // my custom zoom
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [imageScrollView zoomToRect:zoomRect animated:YES];
    [self rotateView:imageScrollView by:M_PI_2];      // then the twist
}

私のローテーションコード...

- (void)rotateView:(UIView *)anImageView by:(CGFloat)spin {

    [UIView animateWithDuration:0.5 delay: 0.0
                        options: UIViewAnimationOptionAllowUserInteraction 
                     animations:^{
                         anImageView.transform = CGAffineTransformMakeRotation(spin);
                     }
                     completion:^(BOOL finished){}];
}

次に、ズームが終了したら、元の座標系の既知の位置にビューを配置します。これは私がいくつかの助けが必要だと思うところです...

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)_scale {

    CGFloat coX = scrollView.contentOffset.x;
    CGFloat coY = scrollView.contentOffset.y;

    // try to build the forward transformation that got us here, then invert it
    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
    CGAffineTransform scale = CGAffineTransformMakeScale(_scale, _scale);

    // should these be negative?  some confusion here.
    CGAffineTransform translate = CGAffineTransformMakeTranslation(coX, coY);

    // what's the right order of concatenation on these?  more confusion
    CGAffineTransform aft0 = CGAffineTransformConcat(rotate, scale);
    CGAffineTransform aft1 = CGAffineTransformConcat(aft0, translate);

    // invert it
    CGAffineTransform aft = CGAffineTransformInvert(aft1);

    // this is the fixed rect in original coordinate system of the image
    // the image is at 0,0 320, 300 in the view, so this will only work
    // when the image is at the origin...  need to fix that later,too.
    CGRect rect = CGRectMake(248, 40, 90, 160);
    CGRect xformRect = CGRectApplyAffineTransform(rect, aft);

    // build a subview with this rect
    UIView *newView = [scrollView viewWithTag:888];
    if (!newView) {
        newView = [[UIView alloc] initWithFrame:xformRect];
        newView.backgroundColor = [UIColor yellowColor];
        newView.tag = 888;
        [scrollView addSubview:newView];
    } else {
        newView.frame = xformRect;
    }
    // see where it landed...
    NSLog(@"%f,%f  %f,%f", xformRect.origin.x, xformRect.origin.y, xformRect.size.width, xformRect.size.height);
}
4

0 に答える 0