我正试图在屏幕上移动UIView。我正在使用UIPanGesture
,以便视图在屏幕上跟随我的手指。我还计算了各种各样的东西,以确定手势的速度,并在此基础上我以该速度动画UIView。
这很有效 - 但它需要触及我想移动的实际UIView。我希望通过触摸主视图并执行仅移动UIView而不是所有视图的手势来获得相同的效果。
以下是移动UIView的代码
-(void)dragging:(UIPanGestureRecognizer *)gesture{
if(gesture.state == UIGestureRecognizerStateBegan)
{
self.panCoord = [gesture locationInView:gesture.view];
}
CGPoint newCoord = [gesture locationInView:gesture.view];
float dX = newCoord.x-self.panCoord.x;
gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y, gesture.view.frame.size.width, gesture.view.frame.size.height);
if (gesture.state == UIGestureRecognizerStateEnded){
methodEnd = [NSDate date];
NSTimeInterval executionTime = [methodEnd timeIntervalSinceDate:methodStart];
double time = executionTime;
float distance = start - end;
int distanceOverTime = distance / time;
float veloticty = distanceOverTime / 100;
double miniTimeToUse = 0.5;
double newMinTime;
if (time < miniTimeToUse){
newMinTime = miniTimeToUse;
}else {
newMinTime = time;
}
NSLog (@"Distance: %f", distance);
NSLog (@"X:%f", gesture.view.frame.origin.x+dX);
if (gesture.view.frame.origin.x+dX >= 90 && distance >= 3){
[UIView animateWithDuration:newMinTime
delay:0
usingSpringWithDamping:0.9
initialSpringVelocity:veloticty
options:0
animations:^{
self.filterView.frame = CGRectMake(300, 0, 300, 500);
} completion:nil];
}
if (gesture.view.frame.origin.x+dX <= 181 ){
[UIView animateWithDuration:newMinTime
delay:0
usingSpringWithDamping:0.9
initialSpringVelocity:veloticty
options:0
animations:^{
self.filterView.frame = CGRectMake(30, 0, 300, 500);
} completion:nil];
}
}
}
如果我将UIPangesture添加到self.filteredView中 - 它可以正常工作。如果我将它添加到self.view - 屏幕上的所有内容都会移动。
我在哪里错了?
答案 0 :(得分:0)
您是否尝试过此方法,只应为可移动视图设置UIPanGestureRecognizer
。
-(void)moveView:(UILongPressGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer locationInView:recognizer.view.superview];
recognizer.view.center = translation;
}
谢谢!
答案 1 :(得分:0)
如果手势附加到self.view
self.view
gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y, gesture.view.frame.size.width, gesture.view.frame.size.height);
您可能希望在那里使用self.filteredView
。