简体   繁体   中英

How to zoomIn ZoomOut inside UIView

I have an ImageView which is add to UIView. Now i need to zoomIn ZoomOut the Image which is added to imageView which again add subview UIView.

now to get zooming effect to the image.

I have try not lucky today.

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

You will want to use a scroll view. Add a scrollview to your nib (or do it programmatically. Then load an UIImage, add it to an UIImageView, then add the UIImageView to the scroll view : //inside you 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];

Then add your gesture methods:

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

EDIT - I had my height and width mixed in my CGSizeMake

Try this,

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);

and put self.multipleTouchEnabled = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM