Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如果没有UIScrollView,如何在UIImageView中进行放大和缩小?_Objective C_Uiimageview_Ios 4.2_Zooming - Fatal编程技术网

Objective c 如果没有UIScrollView,如何在UIImageView中进行放大和缩小?

Objective c 如果没有UIScrollView,如何在UIImageView中进行放大和缩小?,objective-c,uiimageview,ios-4.2,zooming,Objective C,Uiimageview,Ios 4.2,Zooming,我正在为iOS 4.2开发一个应用程序,iPhone,在这个应用程序中我下载图像并将它们保存在内部存储器(NSDocuments)中 那么,我在UIImageView中显示第一个图像。用户可以在UIImageView(触摸移动)上拖动手指,当用户这样做时,我加载其他图像。如果用户向下拖动,我将加载一个图像,如果向上拖动,我将加载另一个图像,还将加载左右图像 这一切都完成了。但我想实现缩放。这是我到目前为止的代码: initialDistance-->是第一次触摸时手指之间的距离 FinalIns

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

那么,我在UIImageView中显示第一个图像。用户可以在UIImageView(触摸移动)上拖动手指,当用户这样做时,我加载其他图像。如果用户向下拖动,我将加载一个图像,如果向上拖动,我将加载另一个图像,还将加载左右图像

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

initialDistance-->是第一次触摸时手指之间的距离
FinalInstance-->是手指每次移动时的距离
x-->为0
y-->为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 -
//此方法计算两个手指之间的距离
-(CGFloat)两点之间的距离:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{
float xPoint=toPoint.x-fromPoint.x;
float yPoint=toPoint.y—fromPoint.y;
返回sqrt(xPoint*xPoint+yPoint*yPoint);
}
//-------------------康洛斯德多斯酒店------------------------------------
#布拉格标记-
#pragma标记应答器
//第一次接触
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
NSSet*AllTouchs=[event AllTouchs];
开关([AllTouchs计数]){
案例1:{//单触
//第一次接触。
UITouch*touch1=[[AllTouchs AllObject]对象索引:0];
开关([touch1 tapCount])
{
案例1://单抽头。
{
//在当地,你可以看到一辆豪华轿车
//inical=[touch1 locationInView:self.view];
}中断;
案例2:{//双击。
//跟踪两个手指之间的初始距离。
//如果([[AllTouchs AllObject]计数]>=2){
//眼睛/不,在多布勒塔普附近
//[自动切换工具栏];
}中断;
}
}中断;
案例2:{//Double-Touch
//距离计算是一种特殊的计算方法
UITouch*touch1=[[AllTouchs AllObject]对象索引:0];
UITouch*touch2=[[AllTouchs AllObject]对象索引:1];
initialDistance=[两点之间的自距离:[触摸1位置查看:[自查看]]
toPoint:[touch2 locationInView:[self view]];
}
违约:
打破
}
}
//当手指移动到
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
NSSet*AllTouchs=[event AllTouchs];
开关([AllTouchs计数])
{
案例1:{
}中断;
案例2:{
//正在放大或缩小图像。
UITouch*touch1=[[AllTouchs AllObject]对象索引:0];
UITouch*touch2=[[AllTouchs AllObject]对象索引:1];
//计算两个手指之间的距离。
CGFloat finalDistance=[两点之间的自我距离:[触摸1位置查看:[自我查看]]
toPoint:[touch2 locationInView:[self view]];
NSLog(@“初始距离:%.f,最终距离:%.f”,初始距离,最终距离);
浮动系数x=20.0;
浮子工厂=11.0;
//2号德多斯酒店
//CGPoint dedo1=[[[touchs allObjects]objectAtIndex:0]locationInView:self.view];
//CGPoint dedo2=[[touchs allObjects]objectAtIndex:1]locationInView:self.view];
//比较刀的大小,放大或缩小
if(初始距离<最终距离){
NSLog(@“放大”);
float newWidth=imagen.frame.size.width+(初始距离-最终距离+系数x);
float newHeight=imagen.frame.size.height+(初始距离-最终距离+工厂);
如果(newWidth=480&&newHeight>=320){
/*
如果(dedo1.x>=dedo2.x){
x=(dedo1.x+最终立场/2);
y=(dedo1.y+最终立场/2);
}否则{
x=(dedo2.x+finalDistance/2);
y=(dedo2.y+最终立场/2);
}
*/
//x-=(最终距离-初始距离+系数x);
//y-=(最终距离-初始距离+系数x);
//x=(dedo1.x);
//y=(dedo1.y);
imagen.frame=CGRectMake(x,y,newWidth,newHeight);
}否则{
imagen.frame=CGRectMake(0,0480320);
}
}
初始距离=最终距离;
}中断;
}
}
#布拉格标记-
非常感谢


PD:如果有一个UISCLVIEW WHTCH的方法,我可以在不同的图像之间移动,我也会打开看看。

OK。如果只使用它的缩放功能,你可以考虑使用UISCLVIEW。 假设我们有一个

scrollView
和一个
imageView
,两者的边界相同。将
imageView
添加为
scrollView
的子视图

[scrollView addSubview:imageView];
scrollView.contentSize = imageView.frame.size;
要在
scrollView
中仅支持缩放而不支持平移,您的
viewController
必须采用
UIScrollViewDelegate
协议

// 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;
}
现在我们已经有了缩放功能。对于滑动,您可以使用适当的配置。创建一个手势来处理每个滑动方向,并将其添加到滚动视图中

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

然后根据手势检索适当的图像,并使用
imageView.image=yourImage;

进行设置。最后,在Deepak的帮助下,我使用UIImageView的transform属性进行缩放

我使用CGAffineTransform矩阵中[0,0]位置的CGFloat来设置限制。我必须在放大时将CGFloat传递给字符串,因为当我将其与0进行比较时,这是alwa
// comparo para saber si el usuario esta haciendo zoom in o zoom out
        if(initialDistance < finalDistance) {
            NSLog(@"Zoom Out");

            CGAffineTransform transformer = CGAffineTransformScale(imagen.transform, 1.05, 1.05);

            NSLog(@"transformer :: A: %.f, B: %.f, C: %.f, D: %.f", imagen.transform.a, imagen.transform.b, imagen.transform.c, imagen.transform.d);

            if (transformer.a < 5) {
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration: 0.2];
                imagen.transform = transformer;
                [UIView setAnimationDelegate:self];
                [UIView commitAnimations];
            }
        }
        else {
            NSLog(@"Zoom In");

            CGAffineTransform transformer = CGAffineTransformScale(imagen.transform, 0.95, 0.95);

            NSLog(@"transformer :: A: %.f, B: %.f, C: %.f, D: %.f", transformer.a, transformer.b, transformer.c, transformer.d);

            NSString *num = [NSString stringWithFormat:@"%.f", transformer.a];

            if (![num isEqualToString:@"0"]) {
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration: 0.2];
                imagen.transform = transformer;
                [UIView setAnimationDelegate:self];
                [UIView commitAnimations];
            }
        }