如何在iCarousel视图中单击一下放大图像?

时间:2016-05-31 10:53:44

标签: ios objective-c uitapgesturerecognizer icarousel

我正努力在单击时缩放图像,但尚未成功。我有一个带有6张图像的旋转木马视图,效果很好,但我想在旋转木马视图中点击图像时缩放/弹出特定图像。任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

首先在TapGestureRegcognizer中声明addSubview然后UIImageView。在viewDidLoad

中使用此代码
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:1];

[imageview addGestureRecognizer:doubleTap];

然后如果单击UIImageView单击,请调用以下方法,

    - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
        // zoom in
        float newScale = [myscrollview zoomScale] * 2;

        if (newScale > self.myscrollview.maximumZoomScale){
            newScale = self.myscrollview.minimumZoomScale;
            CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

            [myscrollview zoomToRect:zoomRect animated:YES];

        }
        else{

            newScale = self.myscrollview.maximumZoomScale;
            CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

            [myscrollview zoomToRect:zoomRect animated:YES];
        }
    }

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [myscrollview frame].size.height / scale;
    zoomRect.size.width  = [myscrollview frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

它为我工作,希望它有用。