在拖动时根据x坐标缩放视图

时间:2014-12-03 22:35:44

标签: objective-c xcode uipangesturerecognizer

我一直在努力实现我在一些使用卡片显示信息的应用程序中看到的用户界面功能。我当前的视图控制器如下所示:

enter image description here

用户可以沿x轴向左和向右拖动卡片。拖动到屏幕的右侧对卡的比例没有任何影响(只是改变位置)但是如果用户向左滑动它我想根据其y坐标慢慢减小它的比例(例如,比例最小当卡片位于最左侧时,从该点开始变大,直到达到原始尺寸为止。如果卡被拖到左侧足够远,它会淡出,但如果用户没有将它拖得足够远,它会增加比例并移回中间。到目前为止,我已尝试过代码:

- (void)handlePanImage:(UIPanGestureRecognizer *)sender
{
    static CGPoint originalCenter;

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        originalCenter = sender.view.center;
        sender.view.alpha = 0.8;
        [sender.view.superview bringSubviewToFront:sender.view];
    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [sender translationInView:self.view];
        NSLog(@"%f x %f y",translation.x ,translation.y);
        sender.view.center=CGPointMake(originalCenter.x + translation.x, yOfView);
        CGAffineTransform transform = sender.view.transform;
        i-=0.001;
        transform = CGAffineTransformScale(transform, i, i);
        //transform = CGAffineTransformRotate(transform, self.rotationAngle);
        sender.view.transform = transform;
    }
    else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
    {
        if(sender.view.center.x>0){
            [UIView animateWithDuration:0.2 animations:^{
                CGRect rect=[sender.view frame];
                rect.origin.x=([self.view frame].size.width/2)-_protoypeView.frame.size.width/2;
                rect.size.height=originalHeight;
                rect.size.width=originalWidth;
                [sender.view setFrame:rect];
                i=1.0;

            }];
        }
        [UIView animateWithDuration:0.1 animations:^{
            sender.view.alpha  = 1.0;
        }];
    }
}

这看起来非常错误并且不能正常工作。我还试图根据翻译改变比例:

else if (sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [sender translationInView:self.view];
        NSLog(@"%f x %f y",translation.x ,translation.y);
        sender.view.center=CGPointMake(originalCenter.x + translation.x, yOfView);
        CGAffineTransform transform = sender.view.transform;
        transform = CGAffineTransformScale(transform, translation.x/100, translation.x/100);
        //transform = CGAffineTransformRotate(transform, self.rotationAngle);
        sender.view.transform = transform;
    }

但是规模太大或太小。任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

在你尝试的最后一段代码中,你做了一些错误的事情。缩放变换将是累积的,因为您从未将平移手势识别器的平移设置回0.此外,如果您在变换中查看数学运算,您将看到说-1点的小运动会缩放视图至原始尺寸的0.01倍。你显然不希望这样。您需要将该小负数添加到1,以便初始-1点移动将缩放到0.99。将panner的转换设置为零的更改需要更改中心计算以使用sender.view.center.x而不是originalCenter.X。您还需要一个if语句来检查中心是否位于其起始位置的左侧或右侧,以便您知道是否应该应用缩放变换。像这样的东西,

-(void)handlePan:(UIPanGestureRecognizer *) sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        originalCenter = sender.view.center;
        sender.view.alpha = 0.8;
        [sender.view.superview bringSubviewToFront:sender.view];

    }else if (sender.state == UIGestureRecognizerStateChanged){
        CGPoint translation = [sender translationInView:self.view];
        sender.view.center=CGPointMake(sender.view.center.x + translation.x, sender.view.center.y);
        if (sender.view.center.x < originalCenter.x) {
            CGAffineTransform transform = sender.view.transform;
            transform = CGAffineTransformScale(transform, 1+translation.x/100.0, 1+translation.x/100.0);
            sender.view.transform = transform;
        }
        [sender setTranslation:CGPointZero inView:self.view];
    }
}

这不会影响视图的动画效果,也不会淡出视图,但它应该可以帮助你完成大部分工作。