我正在开发一个简单的图像交换应用程序,类似于Tinder的用户界面,用户使用平移手势交换/浏览图像。我有一个小问题,这真的让我烦恼,我无法理解为什么会发生这种情况 - 因为我的平移方法正常运行,它似乎仍然不是&作为& #34;平滑"作为火药的。 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]];
}
}];
}
}
}
还有什么我应该做的吗?我在下面有更多的代码来处理图像的速度和位置,但我不认为它与问题有关。我还尝试了其他一些方法来实现&#34拖动&#34;感觉,但他们似乎都不是100%顺利。
任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
经过一些研究,我发现自从我使用iOS的核心图形(圆角,阴影等)绘制相当复杂的视图时,解决方案是将视图的'shouldRasterize'属性设置为YES并修复问题。
谢谢,gl。
答案 1 :(得分: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];