Функциональность фотогалереи - PullRequest
0 голосов
/ 28 февраля 2011

Мне нужно смоделировать эффекты фотогалереи.

У меня есть uiimageview, мне нужно увеличивать и уменьшать масштаб, масштабировать и - если я дважды нажал (или потряс iPad) - я хочу, чтобы изображение вернулось к его фактическому размеру.

Я много пробовал, но все доступные примеры глючат и не завершены; Я не хочу использовать uiwebview.

Любое предложение, пожалуйста?

вот код

имел ввиду загрузку :

holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];


    imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
    [imageview setImage:myimage];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    //holderView.contentMode = UIViewContentModeScaleAspectFit ;
    [holderView addSubview:imageview];

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
    [pinchRecognizer setDelegate:self];
    [holderView addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
    [rotationRecognizer setDelegate:self];
    [holderView addGestureRecognizer:rotationRecognizer];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [holderView addGestureRecognizer:panRecognizer];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    [tapRecognizer setNumberOfTapsRequired:1];
    [tapRecognizer setDelegate:self];
    [holderView addGestureRecognizer:tapRecognizer];

и следующий код, который я использую для функций

-(void)scale:(id)sender {

    [self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

    if(zooming)
    {
        if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            lastScale = 1.0;
            return;
        }

        CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

        CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
        CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

        [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

        lastScale = [(UIPinchGestureRecognizer*)sender scale];

        OldTransform = newTransform;
        OldCenter = [(UIPinchGestureRecognizer*)sender view].center;

        Does_Panned = YES ;
        Does_transformed = YES ;


    }
    else {


    }

}

-(void)rotate:(id)sender {
    if(zooming)
    {

        [self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

        if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            lastRotation = 0.0;
            return;
        }

        CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

        CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
        CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

        [[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

        lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
        OldTransform = newTransform ; 
        OldCenter = [(UIPinchGestureRecognizer*)sender view].center;

        Does_Panned = YES ;
        Does_transformed = YES ; 


    }
}

-(void)move:(id)sender {

    if(zooming)
    {

        [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

        [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
        CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

            firstX = [[sender view] center].x;
            firstY = [[sender view] center].y;
        }

        translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

        [[sender view] setCenter:translatedPoint];

        OldCenter = translatedPoint ;

        Does_Panned = YES ;
        Does_transformed = NO ; 

        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
            CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

            if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {

                if(finalX < 0) {

                    finalX = 0;
                }

                else if(finalX > 768) {

                    finalX = 768;
                }

                if(finalY < 0) {

                    finalY = 0;
                }

                else if(finalY > 1024) {

                    finalY = 1024;
                }
            }

            else {

                if(finalX < 0) {

                    finalX = 0;
                }

                else if(finalX > 1024) {

                    finalX = 768;
                }

                if(finalY < 0) {

                    finalY = 0;
                }

                else if(finalY > 768) {

                    finalY = 1024;
                }
            }

            [UIView beginAnimations:nil context:NULL];
            //[UIView setAnimationDelay: 1.0];
            [UIView setAnimationDuration:.20];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            //[[sender view] setCenter:CGPointMake(finalX, finalY)];
            [UIView commitAnimations];
        }
    }
    else{

        if([(UIPanGestureRecognizer*)sender maximumNumberOfTouches] == 1)
        {



            UIPanGestureRecognizer* recognize = (UIPanGestureRecognizer*)sender; 
            //change the current window width and level 

            if([recognize state] == UIGestureRecognizerStateBegan) {

                Bigen =[recognize translationInView:recognize.view ];// [[sender view] center];
                BeginX = Bigen.x;
                BeginY = Bigen.y;
            }
            else if(

                    ( [recognize state] == UIGestureRecognizerStateChanged )||
                    (recognize.state == UIGestureRecognizerStateEnded)  )
            {



                CGPoint translation =  [recognize translationInView:recognize.view ];

                End = translation ; 

                EndX = End.x ; 
                EndY = End.y ; 

                double offsetw=0;


                double offsetl=0;

                if(EndX>BeginX)
                {
                    offsetw=50*(EndX-BeginX);

                }
                else if(EndX<BeginX)
                {
                    offsetw=50*(EndX-BeginX);

                }
                else
                {
                    offsetw=0;
                }


                if(EndY>BeginY)
                {
                    offsetl=50*(EndY-BeginY);
                }
                else if(EndY<BeginY)
                {
                    offsetl=50*(EndY-BeginY);
                }
                else
                {
                    offsetl=0;
                }


                ChangeWW_Wl(offsetw, offsetl);


                [self AddImage];

            }
            //if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            /* CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

             EndX = translatedPoint.x;
             EndY = translatedPoint.y;*/
            //}




        }



    }
}

(void)tapped:(id)sender {
    if(zooming)
    {

        [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

как обновить его, чтобы сбросить изображение до исходного размера при встряхивании или двойном нажатии

как преодолеть неправильное масштабирование (увеличение с большим коэффициентом в первый раз)

1 Ответ

0 голосов
/ 14 июня 2011

Вы должны посмотреть видео «Проектирование приложений с помощью прокрутки» с WWDC2010, в котором рассказывается, как это сделать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...