如何在UIView内放大ZoomOut

时间:2011-09-06 11:16:11

标签: iphone objective-c

我有一个添加到UIView的ImageView。现在我需要zoomIn ZoomOut添加到imageView的图像,再次添加子视图UIView。

现在可以对图像进行缩放效果。

我今天没试过。

4 个答案:

答案 0 :(得分:1)

尝试在Web视图或滚动视图中添加imageview,然后缩放将很容易。

答案 1 :(得分:1)

您将需要使用滚动视图。将滚动视图添加到您的笔尖(或以编程方式执行。然后加载UIImage,将其添加到UIImageView,然后将UIImageView添加到滚动视图: //在你的viewDidLoad里面

UIImage * image = //set image here
previewImageView = [[UIImageView alloc] initWithImage:image];
scrollView.contentSize = CGSizeMake(320, 480);
[scrollView addSubview:previewImageView];
scrollView.minimumZoomScale = 0.4;
scrollView.maximumZoomScale = 4.0;
scrollView.delegate = self;
[scrollView setZoomScale:scrollView.minimumZoomScale];

然后添加你的手势方法:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollview {
    return self.previewImageView;
}



- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    CGRect zoomRect;
    zoomRect.size.height = [self.scrollView frame].size.height / scale;
    zoomRect.size.width = [self.scrollView frame].size.width / scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
 }

- (void)zoomAction:(UIGestureRecognizer *)gestureRecognizer {

     NSLog(@"Hit the gestureRecognizer");
     float newScale = [self.scrollView zoomScale] * 2;
     CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
     [self.scrollView zoomToRect:zoomRect animated:YES];
 } 

编辑 - 我的CGSizeMake混合了我的身高和宽度

答案 2 :(得分:0)

答案 3 :(得分:0)

试试这个,

UITouch *first = twoTouches objectAtIndex:0; 
UITouch *second = twoTouches objectAtIndex:1; 
CGPoint firstPoint = first locationInView:self;
CGPoint secondPoint = second locationInView:self;
CGFloat initialDistance = distanceBetweenPoints(firstPoint, secondPoint);

并放置self.multipleTouchEnabled = YES;