2

我正在开发一个简单的图像交换应用程序,类似于 Tinder 的 UI,用户使用平移手势交换/导航图像。我遇到了一个小问题,这确实困扰着我,我无法真正理解它为什么会发生 - 由于我的平移方法正常工作,它似乎仍然不像 tinder 那样“流畅”。uiimageview 的动画/重绘没有那么清晰,我不知道为什么。我的代码是相当教科书平移方法:

-(void)HandlePan:(UIGestureRecognizer *)recognizer{


CGPoint translation = [recognizer translationInView:self.view];

[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{[recognizer.view setCenter:(CGPointMake(recognizer.view.center.x+translation.x * 1.50,recognizer.view.center.y +translation.y* 1.50))];} completion:nil];

CGPoint leftOutOfBounds = CGPointMake(-160, 160);
CGPoint rightOutOfBounds = CGPointMake(480, 160);

CGPoint velocity = [recognizer velocityInView:self.view];

if(_imagePreviewerTop.center.x > self.view.center.x){



    if(velocity.x > 0){
         swipeTransform = swipeTransform+.005;
        [_decisionLabel setText:@"UPVOTE"];
        [_decisionLabel setBackgroundColor:[UIColor successColor]];
        [_decisionLabel setTextColor:[UIColor whiteColor]];
    }
    else if (velocity.x < 0){
        swipeTransform = swipeTransform-.005;
    }
    [_imagePreviewerTop setTransform:CGAffineTransformMakeRotation(swipeTransform)];



}
else if(_imagePreviewerTop.center.x < self.view.center.x){
    //NSLog(@"gesture went left");


    if(velocity.x < 0){
        swipeTransform = swipeTransform-.005;
        [_decisionLabel setText:@"PASS"];
        [_decisionLabel setBackgroundColor:[UIColor infoBlueColor]];
        [_decisionLabel setTextColor:[UIColor whiteColor]];
    }
    else if(velocity.x > 0){
        swipeTransform = swipeTransform+.005;
        //NSLog(@"gesture went left");

    }
    [_imagePreviewerTop setTransform:CGAffineTransformMakeRotation(swipeTransform)];


}



[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if(recognizer.state == UIGestureRecognizerStateEnded){
    //implement repositioning here

    if (_imagePreviewerTop.center.x <50){
        //"finger lifted on left"

        [UIView animateWithDuration:.25 animations:^{_imagePreviewerTop.center = leftOutOfBounds;
        } completion:^(BOOL isFinished){
            if (isFinished == true) {
                [self changeImage:@"0"];




            }
        }];
    }


    else if (_imagePreviewerTop.center.x > 270){
        //"finger lifted on right"
        //[self.view addSubview:_upVote];
        [UIView animateWithDuration:.25 animations:^{_imagePreviewerTop.center = rightOutOfBounds;             } completion:^(BOOL isFinished){
            if (isFinished == true) {
                [self changeImage:@"1"];

            }
        }];
    }
    else{

        [UIView animateWithDuration:.5 animations:^{_imagePreviewerTop.center =_imagePreviewerBottom.center;[_imagePreviewerTop setTransform:CGAffineTransformMakeRotation(0.0)];swipeTransform = 0;} completion:^(BOOL isFinished){
            if (isFinished == true) {
                [_decisionLabel setText:@"start swiping!"];
                [_decisionLabel setBackgroundColor:[UIColor babyBlueColor]];
                [_decisionLabel setTextColor:[UIColor infoBlueColor]];

            }
        }];
    }
}


}

还有什么我应该做的吗?我在下面有更多代码处理图像的速度和位置,但我认为它与问题无关。我还尝试了其他一些实现“拖动”感觉的方法,但它们似乎都没有100% 顺利。

任何帮助将不胜感激,谢谢!

4

2 回答 2

2

在做了一些研究之后,我发现由于我使用 iOS 的核心图形(圆角、阴影等)绘制了非常复杂的视图,因此解决方案是将视图的“shouldRasterize”属性设置为 YES,从而解决了问题。

谢谢和gl。

于 2013-08-12T23:08:04.417 回答
1

尝试像这样为运动设置动画:

[UIView animateWithDuration:0.1 
                      delay:0.0 
                    options:UIViewAnimationCurveEaseInOut 
                 animations:^ {
                   [recognizer.view setCenter:CGPointMake(recognizer.view.center.x+translation.x * 1.50,recognizer.view.center.y +translation.y* 1.50)];
                 }
                 completion:nil];
于 2013-08-12T17:21:21.730 回答