<bdo id='hGPJ0'></bdo><ul id='hGPJ0'></ul>
<legend id='hGPJ0'><style id='hGPJ0'><dir id='hGPJ0'><q id='hGPJ0'></q></dir></style></legend>

    <i id='hGPJ0'><tr id='hGPJ0'><dt id='hGPJ0'><q id='hGPJ0'><span id='hGPJ0'><b id='hGPJ0'><form id='hGPJ0'><ins id='hGPJ0'></ins><ul id='hGPJ0'></ul><sub id='hGPJ0'></sub></form><legend id='hGPJ0'></legend><bdo id='hGPJ0'><pre id='hGPJ0'><center id='hGPJ0'></center></pre></bdo></b><th id='hGPJ0'></th></span></q></dt></tr></i><div id='hGPJ0'><tfoot id='hGPJ0'></tfoot><dl id='hGPJ0'><fieldset id='hGPJ0'></fieldset></dl></div>
  1. <tfoot id='hGPJ0'></tfoot>

    1. <small id='hGPJ0'></small><noframes id='hGPJ0'>

    2. 如何在没有 UIScrollView 的情况下放大和缩小 UIImageView?

      时间:2024-04-15
      <tfoot id='oqwc3'></tfoot>
        <bdo id='oqwc3'></bdo><ul id='oqwc3'></ul>
            <legend id='oqwc3'><style id='oqwc3'><dir id='oqwc3'><q id='oqwc3'></q></dir></style></legend>

            <small id='oqwc3'></small><noframes id='oqwc3'>

                  <tbody id='oqwc3'></tbody>
                <i id='oqwc3'><tr id='oqwc3'><dt id='oqwc3'><q id='oqwc3'><span id='oqwc3'><b id='oqwc3'><form id='oqwc3'><ins id='oqwc3'></ins><ul id='oqwc3'></ul><sub id='oqwc3'></sub></form><legend id='oqwc3'></legend><bdo id='oqwc3'><pre id='oqwc3'><center id='oqwc3'></center></pre></bdo></b><th id='oqwc3'></th></span></q></dt></tr></i><div id='oqwc3'><tfoot id='oqwc3'></tfoot><dl id='oqwc3'><fieldset id='oqwc3'></fieldset></dl></div>

              1. 本文介绍了如何在没有 UIScrollView 的情况下放大和缩小 UIImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在为 iOS 4.2、iPhone 开发一个应用程序,在这个应用程序中我下载图像并将它们保存在内部存储 (NSDocuments) 中.

                I'm developing an app for iOS 4.2, iPhone, in this app I download images and I save them in the internal storage (NSDocuments).

                那么,我在 UIImageView 中显示第一张图片.用户可以在 UIImageView (TouchesMoved) 上拖动手指,当用户这样做时,我会加载其他图像.如果用户向下拖动,我会加载一张图片,如果向上拖动,我会加载另一张图片,并且左右也可以.

                Well, then I show the first image in a UIImageView. The user can drag their finger on the UIImageView (TouchesMoved), when the user do that, I load other image. If the user drag down, I load one image, if drag up, i load other, and also with right and left.

                这一切都完成了.但我想实现缩放.到目前为止,这是我的代码:

                This is all done. But I want to implement zooming. This is my code until now:

                initialDistance --> 是第一次触摸时手指之间的距离
                finalDistance --> 是手指每次移动时的距离
                x --> 是 0
                y --> 为 0

                initialDistance --> is the distance between the fingers at first touch
                finalDistance --> is the distance between the fingers each time they move
                x --> is 0
                y --> is 0

                    // this method calculate the distance between 2 fingers
                    - (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
                        float xPoint = toPoint.x - fromPoint.x;
                        float yPoint = toPoint.y - fromPoint.y;
                
                        return sqrt(xPoint * xPoint + yPoint * yPoint);
                     }
                
                        //------------------- Movimientos con los dedos ------------------------------------
                        #pragma mark -
                        #pragma mark UIResponder
                
                          // First Touch
                -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
                
                NSSet *allTouches = [event allTouches];
                
                switch ([allTouches count]) {
                    case 1: { //Single touch
                
                        //Get the first touch.
                        UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
                
                        switch ([touch1 tapCount])
                        {
                            case 1: //Single Tap.
                            {
                                // Guardo la primera localización del dedo cuando pulsa por primera vez
                                //inicial = [touch1 locationInView:self.view];
                
                            } break;
                            case 2: {//Double tap. 
                                //Track the initial distance between two fingers.
                                //if ([[allTouches allObjects] count] >= 2) {
                
                                // oculto/o no, la barra de arriba cuando se hace un dobleTap
                                //[self switchToolBar];
                
                            } break;
                        }
                    } break;
                    case 2: { //Double Touch
                
                        // calculo la distancia inicial que hay entre los dedos cuando empieza a tocar
                        UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
                        UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
                
                        initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]]
                                                                 toPoint:[touch2 locationInView:[self view]]];
                    }
                    default:
                        break;
                }
                }
                
                // when the finger/s move to 
                -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
                {
                
                NSSet *allTouches = [event allTouches];
                
                switch ([allTouches count])
                {
                    case 1: {
                
                
                
                    } break;
                    case 2: {
                        //The image is being zoomed in or out.
                
                        UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
                        UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
                
                        //Calculate the distance between the two fingers.
                        CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]]
                                                                       toPoint:[touch2 locationInView:[self view]]];
                
                        NSLog(@"Distancia Inicial :: %.f, Ditancia final :: %.f", initialDistance, finalDistance);
                
                        float factorX = 20.0;
                        float factorY = 11.0;
                
                        // guardo la posicion de los 2 dedos
                        //CGPoint dedo1 = [[[touches allObjects] objectAtIndex:0] locationInView:self.view];
                        //CGPoint dedo2 = [[[touches allObjects] objectAtIndex:1] locationInView:self.view];
                
                        // comparo para saber si el usuario esta haciendo zoom in o zoom out
                        if(initialDistance < finalDistance) {
                            NSLog(@"Zoom In");
                
                            float newWidth = imagen.frame.size.width + (initialDistance - finalDistance + factorX);
                            float newHeight = imagen.frame.size.height + (initialDistance - finalDistance + factorY);
                
                            if (newWidth <= 960 && newHeight <= 640) {
                                /*
                                 if (dedo1.x >= dedo2.x) {
                                 x = (dedo1.x + finalDistance/2); 
                                 y = (dedo1.y + finalDistance/2);
                                 } else {
                                 x = (dedo2.x + finalDistance/2); 
                                 y = (dedo2.y + finalDistance/2);
                                 }
                                 */
                
                                //x = (dedo1.x);
                                //y = (dedo1.y);
                
                                imagen.frame = CGRectMake( x, y, newWidth, newHeight);
                            } else {
                                imagen.frame = CGRectMake( x, y, 960, 640);
                            }
                
                
                
                        }
                        else {
                            NSLog(@"Zoom Out");
                
                            float newWidth = imagen.frame.size.width - (finalDistance - initialDistance + factorX);
                            float newHeight = imagen.frame.size.height - (finalDistance - initialDistance + factorY);
                
                            if (newWidth >= 480 && newHeight >= 320) { 
                                /*
                                 if (dedo1.x >= dedo2.x) {
                                 x = (dedo1.x + finalDistance/2); 
                                 y = (dedo1.y + finalDistance/2);
                                 } else {
                                 x = (dedo2.x + finalDistance/2); 
                                 y = (dedo2.y + finalDistance/2);
                                 }
                                 */
                                //x -= (finalDistance - initialDistance + factorX); 
                                //y -= (finalDistance - initialDistance + factorX); 
                
                                //x = (dedo1.x);
                                //y = (dedo1.y);
                
                                imagen.frame = CGRectMake( x, y, newWidth, newHeight);
                            } else {
                                imagen.frame = CGRectMake( 0, 0, 480, 320);
                            }
                
                
                
                        }
                
                        initialDistance = finalDistance;
                
                    } break;
                }
                }
                
                #pragma mark -
                

                非常感谢!!

                PD:如果有 UIScrollView 的方法可以在不同的图像之间移动,我也愿意看看.

                PD: If there is a method with UIScrollView whitch I can move between different images, I m open to take a look too.

                推荐答案

                好的.如果只是将它用于缩放功能,您可以考虑使用 UIScrollView.

                Ok. You can consider using a UIScrollView if only to use it for its zoom functionality.

                假设我们有一个 scrollView 和一个 imageView 两者具有相同的边界.添加 imageView 作为 scrollView 的子视图.

                Say we have a scrollView and a imageView with both having the same bounds. Add the imageView as the subview of scrollView.

                [scrollView addSubview:imageView];
                scrollView.contentSize = imageView.frame.size;
                

                要在 scrollView 中仅支持缩放而不支持平移,您的 viewController 必须采用 UIScrollViewDelegate 协议.

                To support only zoom and not not panning in the scrollView your viewController will have to adopt the UIScrollViewDelegate protocol.

                // Disabling panning/scrolling in the scrollView
                scrollView.scrollEnabled = NO;
                
                // For supporting zoom,
                scrollView.minimumZoomScale = 0.5;
                scrollView.maximumZoomScale = 2.0;
                
                ...
                
                // Implement a single scroll view delegate method
                - (UIView*)viewForZoomingInScrollView:(UIScrollView *)aScrollView {
                    return imageView;
                }
                

                现在我们可以进行缩放了.对于滑动,您可以使用适当配置的 UISwipeGestureRecognizers.创建用于处理每个滑动方向的手势并将其添加到滚动视图.

                By now we have zooming available. For swipes, you can use appropriately configured UISwipeGestureRecognizers. Create a gesture for handling each swipe direction and add it to scroll view.

                UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self selector:@selector(handleRightSwipe:)];
                rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
                rightSwipe.numberOfTouchesRequired = 1;
                [scrollView addGesture:rightSwipe];
                [rightSwipe release];
                

                并根据手势检索适当的图像并使用 imageView.image = yourImage; 进行设置.

                And retrieve the appropriate image based on the gesture and set it using imageView.image = yourImage;.

                这篇关于如何在没有 UIScrollView 的情况下放大和缩小 UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:CALayer 性能与 UIImageView 性能 下一篇:如何将多个 UIImageView 合并为一个 UIImage

                相关文章

                <i id='Bz0H3'><tr id='Bz0H3'><dt id='Bz0H3'><q id='Bz0H3'><span id='Bz0H3'><b id='Bz0H3'><form id='Bz0H3'><ins id='Bz0H3'></ins><ul id='Bz0H3'></ul><sub id='Bz0H3'></sub></form><legend id='Bz0H3'></legend><bdo id='Bz0H3'><pre id='Bz0H3'><center id='Bz0H3'></center></pre></bdo></b><th id='Bz0H3'></th></span></q></dt></tr></i><div id='Bz0H3'><tfoot id='Bz0H3'></tfoot><dl id='Bz0H3'><fieldset id='Bz0H3'></fieldset></dl></div>

                    • <bdo id='Bz0H3'></bdo><ul id='Bz0H3'></ul>

                    <small id='Bz0H3'></small><noframes id='Bz0H3'>

                  1. <legend id='Bz0H3'><style id='Bz0H3'><dir id='Bz0H3'><q id='Bz0H3'></q></dir></style></legend>
                    <tfoot id='Bz0H3'></tfoot>